Open external links in new tab or open external links in new window automatically using jquery

 

How to open external links in new tab or how to open external links in new window automatically using jquery?

Today, when I am working with the link building for my blog than suddenly one new thought come in my mind that when we include external links in our web-site, and readers redirect on those links from our web-site. This way we can loose the readers from our web-site and may be they will not come back from that.

One way to keep the readers on our web-site is to open all the external links in new tabs. Or open all the external links in new windows. This means open external link in separate tab or open external link in separate window.
There are two way to fix this issue, either you can do it automatically using the jquery or you can give target _blank in all <a> tags in the content of each web page as well as all the links in your website navigation.

I don’t think that I need to explain regarding how to open external links in new tab or how to open external links in new window in <a> tag.
You can do it automatically using jquery code which is as below.

//Open external links in new tab or open external links in new window automatically using jquery
 
$(function($){
    $('a[href^="http://"]')
        .not('[href*="freelancephpdevelopment.com "]')
        .attr('target','_blank');
});

You need to add this code in your page in script tags, make sure that you change the domain name or url which is “freelancephpdevelopment.com” in this code with your domain name or url. Please let me know if you need more help to fix this type of issues.