[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Make all links open in a new window?

Posted on 2004-11-16
11
Medium Priority
?
346 Views
Last Modified: 2008-03-03
How do I make all links i.e. <a href> to open in a new window? Either through JavaScript? PHP headers? CSS?
0
Comment
Question by:lifegauge
8 Comments
 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 12592943
simple html:
<a href="mylink.html" target="_blank">MY LINK</a>
0
 
LVL 10

Expert Comment

by:eeBlueShadow
ID: 12609398
<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
 
LVL 1

Expert Comment

by:rockstar_dub
ID: 12609822
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:gruntar
ID: 12643138
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
 
LVL 10

Accepted Solution

by:
eeBlueShadow earned 200 total points
ID: 12647809
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
 
LVL 9

Expert Comment

by:gruntar
ID: 12648075
Why? Well, you have already answered yourself. W3C standards.

Regards
0
 
LVL 10

Expert Comment

by:eeBlueShadow
ID: 12826372
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
 
LVL 9

Expert Comment

by:gruntar
ID: 12827287
:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question