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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.