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?
lifegaugeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Oliver_DornaufCommented:
simple html:
<a href="mylink.html" target="_blank">MY LINK</a>
0
eeBlueShadowCommented:
<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
0
rockstar_dubCommented:
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...
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

gruntarCommented:
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
0
eeBlueShadowCommented:
gruntar, why clog up your page with an extra 13 lines of JavaScript (which limits you not only to JavaScript enabled browsers, but to browsers running a version of DOM) when one line of HTML is all that is needed.

The <base> tag has been supported since early versions of HTML, and means you don't have to rewrite any existing links. If you want the page to be W3C validated, you need an href attribute in there, which should be the folder the page is in:

<base href='http://www.mysite.com/thisDir/' target='_blank' />

If you aren't interested in W3C conformance, then the tag as in my first post will work just fine

_Blue
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gruntarCommented:
Why? Well, you have already answered yourself. W3C standards.

Regards
0
eeBlueShadowCommented:
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)
0
gruntarCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.