Link to home
Start Free TrialLog in
Avatar of lifegauge
lifegauge

asked on

Make all links open in a new window?

How do I make all links i.e. <a href> to open in a new window? Either through JavaScript? PHP headers? CSS?
Avatar of Oliver_Dornauf
Oliver_Dornauf
Flag of Afghanistan image

simple html:
<a href="mylink.html" target="_blank">MY LINK</a>
Avatar of eeBlueShadow
eeBlueShadow

<base target='_blank' />

The above can go anywhere in you document, but in the <head> section is better. Plus, I'm not sure how base fits into the newer standards.

_Blue
Here you go, this is how I would do it
make all of your links like this:

<a href="mylink.html" target="<? echo $target ?>">MY LINK</a>


Then put this little php in the very top of your page
even above <html>,
we'll use $_GET (so the condition is in the url)
you could also use $_POST or even $_COOKIE


if ($_GET[win]) {
     $target = "_blank"
} else {
     $target = "_self"
}


so if the url looks like this
http://www.site.com/?win=new
then all of the links will open in new pages.

and if the url looks like this
http://www.site.com/
then all of the links will not open in new pages.

so if the url looks like this
http://www.site.com/?win=anything
then all of the links will open in new pages.

Hope this helps...
Here you have Javascript version
Put this script in head of your html page

<script language="JavaScript">
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "new")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;
</script>

than links that you want to open in new window would be like this
<a href="somelink.php" rel="new">text</a>

Cheers
ASKER CERTIFIED SOLUTION
Avatar of eeBlueShadow
eeBlueShadow

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why? Well, you have already answered yourself. W3C standards.

Regards
The answer given in http:#12609398 is easily the shortest solution and will work as of HTML 2

As it stands it doesn't validate HTML 4 upwards, but http:#12647809 shows the full version of the same tag which does validate.

Egotistical as it sounds, Recommend
    Accept: BlueShadow (http:#12647809)