stickynote427 Posted May 17, 2009 Share Posted May 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158505-automatically-setting-links-target-to-_blank-if-it-doesnt-point-to-my-site/ Share on other sites More sharing options...
Masna Posted May 17, 2009 Share Posted May 17, 2009 How are you generating these mysterious links? Are they all generated using PHP? Or, are you manually, statically placing them in various positions throughout your site? Quote Link to comment https://forums.phpfreaks.com/topic/158505-automatically-setting-links-target-to-_blank-if-it-doesnt-point-to-my-site/#findComment-835943 Share on other sites More sharing options...
BK87 Posted May 17, 2009 Share Posted May 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/158505-automatically-setting-links-target-to-_blank-if-it-doesnt-point-to-my-site/#findComment-835956 Share on other sites More sharing options...
stickynote427 Posted May 18, 2009 Author Share Posted May 18, 2009 @BK87: The method you provided is acceptable, I believe. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/158505-automatically-setting-links-target-to-_blank-if-it-doesnt-point-to-my-site/#findComment-836108 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.