Jump to content

Automatically setting link's TARGET to _blank if it doesn't point to my site


stickynote427

Recommended Posts

Is there some way in PHP (or some other language maybe?) that I can automatically set a link's TARGET attribute to _blank if that link does not point to a page on my site?

 

For example, if I have <a href="http://www.mysite.com/">My Site</a>, it should be left alone, but if I have <a href="http://www.phpfreaks.com">PHP Freaks</a>, it should automatically open in a new window when clicked.

 

Any information is helpful.

 

Thanks.

Blake

there definitely is possible with php because I used this method before on my old websites, although! Its much easier with javascript!

 

<Script>
// change your domain name:
var yourURL = "localhost";
function outLinks() {
  var outLink;
  if (document.getElementsByTagName('a')) {
    for (var i = 0; (outLink = document.getElementsByTagName('a')[i]); i++) {
      if (outLink.href.indexOf(yourURL) == -1) {
        outLink.setAttribute('target', '_blank');
      }
    }
  }
}
window.onload = function() {
    outLinks();
}
</script>

(also I take no credit for this found this online)

just place anywhere in header section of file, and change localhost to youwebsite.com

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.