Skip to main content

jQuery to Auto Open all External Links in new Window

open all external links in new windowTired of putting target="_blank" to every external link in your blog to make them in a new window when clicked. Here’s a simple jQuery script to automatically open all your external links in a new window. The script detects external hostnames and will automatically +add target="_blank" attribute to the external link when clicked. Thus making your readers stay in your blog itself and thus reducing bounce rates and increasing pages views.
The script works in both blogger and wordpress.




HOW TO OPEN ALL YOUR EXTERNAL LINKS IN A NEW TAB

1. Navigate to Blogger Dashboard > Template
2. Now choose Edit HTML. (Note: Always do a backup while editing your template)
3. Search for </head> and just above it paste the below code.
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script> <script type='text/javascript'>
$(document).ready(function() {
  $("a[href^='http://']").each(
    function(){
     if(this.href.indexOf(location.hostname) == -1) {
        $(this).attr('target', '_blank');
      }
    }
  );
$("a[href^='https://']").each(
function(){
if(this.href.indexOf(location.hostname) == -1) {
$(this).attr('target', '_blank');
}
}
);
 
});
</script>
4. Save the template.
NOTE 1: Do not add the bolded code if your blog template already have jQuery library linked in.
NOTE 2: If you change == -1) in the code highlighted in yellow to == 1) then all your internal links will start opening in a new tab.
That’s it. Just check your blog and click on any external link to see it working. Hope this was helpful.
Credits: This script was originally shared by Mohammad Mustafa Ahmedzai of MBT. You should thank him. Peace:)

Comments

Popular posts from this blog

Getting to know Blogger's Post Editor - Beginner's Guide

Blogger provides us with a simple and clean post editor, which can be used by anyone with ease. It is simple and equally efficient at the same time. Comparing with WordPress post editor, one will miss a lot of buttons. But using Blogger's post editor will prove you that those missing buttons were useless. Blogger'a post editor has not too many format options, but has everything one needs. I'm quite sure that even a beginner won't find the editor confusing. But what if someone (a starter) needs to know what exactly each buttons stands for before beginning to use it. This post is for them and only for them.(i know no one else will stop to read this post)

Automatic Read More With Thumbnails for Blogger

Blogger gives us an option to add jump break to posts. It enables us to display a a short summary of the post on the homepage of our blog with a link to the full post. Most of us add jump break to our posts, because displaying a short summary instead of the full post on the main page has many advantages. The main reason is that it increases page views and the visitors can easily get a selection of your posts.

Uploading a Photo to Blogger Posts - Beginner's Guide

Ok! I know this is simple. This post is just to help the beginners and as a part of the "beginner guide from BloggsLab. Every post in a blog should have a photo in it (recommended!). Inserting a photo to your blog post is easy as said. Blogger's post editor gives is simple and even simple is the upload menu. So, evenit is simple, some might find it tricky (don't believe me). So below in this post you can read on uploading an image through blogger post editor. Read on...