Skip to content Skip to sidebar Skip to footer

Jquery Add Target="_blank" For Outgoing Link

I need some help to create jquery script :) I have some of link like this on my HTML. GoogleHome

Solution 1:

assuming that all external links will start with http:// you could do this:

$('a[href^="http://"]').not('a[href*=gusdecool]').attr('target','_blank');

Solution 2:

Solution 3:

$('a[href^=http]:not([href^=http://www.gusdecool.com/])').attr('target','_blank');

Of course, this works only if all the external links start with the http protocol. You should adapt this code to suite your needs (suchs as links without protocols, or with different protocols).

UPDATE :

$('a[href^=http]:not([href^=http://www.gusdecool.com],[href^=http://gusdecool.com])')
    .add('a[href^=www]:not([href^=www.gusdecool.com])')
        .attr('target','_blank');

It selects all the a elements that have their href attribute starting with a web page address (with or without protocol) and do not point to your site's address and changes their target attribute to _blank.

Solution 4:

This function seems to be easier if you have a subdomain:

$('a').attr('target', function() {
  if(this.host == location.host) return'_self'elsereturn'_blank'
});

Solution 5:

jQuery(document).ready(function(){
    target_luar();
});    
function target_luar(){
    try{
        if(top.location != location) {
            jQuery("a[href^='http']")
              .not("[href*='"+location.host+"']")
              .attr('target','_blank');
        }
    } catch(err) { }
}

Demo : Demo jQuery External Link

Post a Comment for "Jquery Add Target="_blank" For Outgoing Link"