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.

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)

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.