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

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.

Responsive Recent Posts Slider Widget for Blogger

Here is one amazing responsive featured post slider for your blogs. This slider is Here i am with a fully automated featured posts slider for blogger blogs. This widget features a complete responsive design and is coded to fetch the most recent posts automatically from your blog. Yes i said fully automated, you only need to add your blog url in the code.

Design a Fancy Accordion Widget For Blogger

Accordion widgets are really helpful in organizing links in your blog. You can use it to list out popular posts in categories in the form of an expandable and collapsible menu. It is an effective and neat way of listing longs list of links.