Skip to main content
added 57 characters in body; added 255 characters in body; added 133 characters in body
Source Link
Lightness Races in Orbit
  • 386.8k
  • 77
  • 670
  • 1.1k

www.yourhost.com is not the same as yourhost.com, so when your links don't match, this isn't working.

You can just take out the www. if you know that this will always lead to a valid URI.

(Also, your use of .each is almost redundant, as jQuery is already knowledgeable about element sets; however, you need it for this.href. Just something to be aware of.)

$("a").filter(function() { return this.hostname && this.hostname.replace(/^www\./, '') !== location.hostname.replace(/^www\./, ''); }).each(function() {   $(this).attr({   target: "_blank",   title: "Visit " + this.href + " (click to open in a new window)" }); }); 

Live example.

$("a").filter(function() { return this.hostname && this.hostname.replace(/^www\./, '') !== location.hostname.replace(/^www\./, ''); }).each(function() {   $(this).attr({   target: "_blank",   title: "Visit " + this.href + " (click to open in a new window)" }); }); 

www.yourhost.com is not the same as yourhost.com, so when your links don't match, this isn't working.

You can just take out the www. if you know that this will always lead to a valid URI.

(Also, your use of .each is almost redundant, as jQuery is already knowledgeable about element sets; however, you need it for this.href. Just something to be aware of.)

$("a").filter(function() { return this.hostname && this.hostname.replace(/^www\./, '') !== location.hostname.replace(/^www\./, ''); }).each(function() { $(this).attr({ target: "_blank", title: "Visit " + this.href + " (click to open in a new window)" }); }); 

Live example.

Source Link
Lightness Races in Orbit
  • 386.8k
  • 77
  • 670
  • 1.1k

$("a").filter(function() { return this.hostname && this.hostname.replace(/^www\./, '') !== location.hostname.replace(/^www\./, ''); }).each(function() { $(this).attr({ target: "_blank", title: "Visit " + this.href + " (click to open in a new window)" }); });