Open External Link in New Tab

Opening external link in new tab is ciritical and somewhat important too. So, Below is code, it will automatically open all external link in new tab.

var a = new RegExp('/' + window.location.host + '/');
jQuery('a').each(function() { 
    if(!a.test(this.href) && this.href != '#' && this.href != 'javascript:void(0)') {
        jQuery(this).click(function(event) {
            event.preventDefault();event.stopPropagation();
            window.open(this.href, '_blank'); 
        });
    }
});

It will match and test hostname in link with current page using javascript `regex` , `test` function . If they don't match and while they are not "#" or "javascript:void(0)" either.

It will open all them in new TAB.

 


Loading ...

Related Results :

  1. Open External Link in New Tab
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author