--> Automatically Open all External Links in a New Window in Blogger! | Experience Lab - Online business creation and development guide for bloggers and startups

Automatically Open all External Links in a New Window in Blogger!

Just today a client requested us that he wish to open all his external links in new window or new tab when clicked and he is not willing to ...

automatically open links in new windowJust today a client requested us that he wish to open all his external links in new window or new tab when clicked and he is not willing to manually do it for each post by adding target="_blank" inside each hyperlink. We wrote down a simple jQuery script that will check all hyperlinks inside your blog pages and will only add the target="_blank"  attribute to links which are external links. It wont  put any effect to internal links of the blog. The script will only detect external hostnames and the moment it finds one, it will insert into the html a target attribute that tells the browser to open the link in a new window. Thus preventing your blog readers from leaving your blog and also helps in increasing your overall pageviews and bounce rate. We thought we shall also share the script with all of you. 

UPDATE: A more updated version of this script has been released. Find how to stop internal links from opening in new window and only open external links in new window.

Open all external links in a new tab

Follow these easy steps to add this cool dynamic way of updating all external links.

Note: This method works for both blogger and wordpress blogs, irrespective of platforms.

  1. Go to Blogger > Template
  2. Backup your template
  3. Click "Edit HTML"
  4. Just above </head> paste the following script:

<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>

PS: Please do not add the red code if you already have jQuery library source code installed on your blog.

     5. Save your template and you are all done!

Check your blog and click some external links to see it working just fine. :)

How it works? (Optional know-how)

I have added the code twice to check once for links starting with http://  and then for https://. Normally all bloggers blogs have hostnames starting with http://  without the http:// secure protocol. Therefore I added the second instance so that the script may work even for wordpress self hosted blogs.

The yellow highlighted part is where the script scans values inside href attributes and if the value is equal to a negative one (== -1), then this indicates that the hostname is an external domain.

Note: If you equate the href value to a positive one (==1) inside the conditional statement, then all your internal links will start opening in new windows!

Once the condition is met, the script then inserts a target="_blank" attribute inside the hyperlink tag. That simple!

Hope this script proves helpful for most of you. It is extremely SEO friendly and works fine with all major browsers be it IE, webkit browsers or Mozilla.

Peace and blessings buddies! :)

COMMENTS

Name

Affiliate Marketing,12,Announcement,34,Bing,9,Bitcoin,38,blog,7,Blogger Resources,42,Blogger Templates,4,blogger tricks,156,Blogging ethics,70,Blogging tips,198,Bugs and Errors,34,Business,10,Copyright Violation,9,CSS and HTMLTricks,95,Designs,8,drop down menu,7,eBook,12,Email Marketing,7,Events,30,Facebook,30,Facebook tricks,49,Google,157,Google AdSense,42,Google Analytics,7,Google Plus,51,Google Plus Tricks,38,Guest Posts,112,home,2,How To,77,Internet,1,JSON Feeds,25,Kitchen Recipes,2,Label Based Sitemap Themes,1,Make Money Online,108,Marketing,17,MBT Blogger Templates,7,Menus,1,News,146,Pages,1,Posts,10,presentations,15,Responsive,10,Reviews,7,SEO,307,Settings,6,Shortcode,15,Sitemap Themes,1,Social Media,155,Technology,7,Templates,1,Tips,2,Tools,1,Traffic Tips,80,Video,19,Web Designing,62,web hosting,18,Webmaster Tools,97,Widgets,199,wordpress,26,
ltr
item
Experience Lab - Online business creation and development guide for bloggers and startups: Automatically Open all External Links in a New Window in Blogger!
Automatically Open all External Links in a New Window in Blogger!
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpFn7SFLl8rQKtygC1V0L0-U0FCGl-M5nwiBlUmtt7YXHjBh4wp-f7GgPqmkTjleFlL7ekV1RT0ONT7FcxMqRoAMGXx7dInAH8TIKOZkIYsFww-zHzzwtdcXd_JwV8PVYp37ij7YSxPn4/?imgmax=800
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpFn7SFLl8rQKtygC1V0L0-U0FCGl-M5nwiBlUmtt7YXHjBh4wp-f7GgPqmkTjleFlL7ekV1RT0ONT7FcxMqRoAMGXx7dInAH8TIKOZkIYsFww-zHzzwtdcXd_JwV8PVYp37ij7YSxPn4/s72-c/?imgmax=800
Experience Lab - Online business creation and development guide for bloggers and startups
https://www.experiencelab.info/2013/12/automatically-open-all-external-links.html
https://www.experiencelab.info/
https://www.experiencelab.info/
https://www.experiencelab.info/2013/12/automatically-open-all-external-links.html
true
2959477579779989044
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share. STEP 2: Click the link you shared to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy