Help - Search - Members - Calendar
Full Version: Links In A New Window With Xhtml?
Zymic Webmaster Forums > Web Design & Development > Client Side Scripting > HTML and CSS
Daniel
Sorry if i've doubled up here, i had a quick search and couldn't find anything that matches exactly.

The XHTML spec has target="_blank" removed, and there seems to be a couple of work arounds on the web. Now please, i don't want a flame war about how users hate links that open in a new window (i'm using it to link to a full scale image from a thumbnail), i'd like to discuss the best way to do it whilst keeping valid code and compatability accross the board.

Sitepoint's tutorial suggests using javascript to re-write the tag after the browser has rendered it, i.e. so that it passes validation but still works for the user (so long as they have javascript, otherwise it falls back to a same window link). I'm not sure this is the best way to do this.
CODE
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") == "external" )
        {
            anchor.target = "_blank";
        }
    }
}
window.onload = externalLinks;


Any thoughts and discussions about how you would/are doing it would be greatly appreciated.

EDIT: The code does work in IE7, was a syntax error on my part.
MrTouz
try :

CODE
target="new"


It works but i dunno if its valid ?
Magnus
There is a reason target was removed from the XHTML specification. It is not a good practice to open all links in a new window, that is changing default UA behavior. You should never try to change the behavior of a browser's interface when coding.
wozzym
QUOTE(Magnus @ Aug 17 2008, 10:15 AM) *
There is a reason target was removed from the XHTML specification. It is not a good practice to open all links in a new window, that is changing default UA behavior. You should never try to change the behavior of a browser's interface when coding.


you should but that on your new site wink.gif
Jetteh22
He specifically asked for no "flame wars" on why they shouldn't be opened in new windows.
Yet magnus still did so. While you may not be flaming, you're in no way helping the person whatsoever.

He did say that he knows they shouldn't be opened in new windows but he is opening them for a reason which he feels is right. I don't disagree. Links should open in new windows if 1) They're opening a larger picture or 2) They are opening an advertisers page when the advertisement is clicked on. This ensures that the user will not accidentally leave your site. smile.gif

And sorry but I don't know exactly how to do this though I think that tutorial you listed might be a good bet to start out w/.
Magnus
There is a reason this was removed. Do not try and alter default user agent behavior under ANY circumstances.
MrTouz
I don't think he cares about the reason, he just wants a valid way to do it tongue.gif
Magnus
There isn't a valid way to do it ":P" because the W3 Consortium decided it didn't want people to be able to change the way a browser behaves
Andrew
Is there really a point to arguing about this? There technically is a way he can do it and it will still come up valid in a validity checker, so why don't we help him find the work around and stop fighting about whether or not a group of guys on a website like whether or not we open new windows.
Magnus
http://magnusfx.com/?p=106
eNVoXia
I understand that W3C decided that the target value was improper for the reason not to change the browsers behavior. While on a personal computer, this doesn't make too much sense because the target="_blank" or the target="new" attribute does what it is supposed to do but consider that it may not on a mobile phone or other device. Personally, as long as you're not completely butchering the code and your only short a by a few minor validations, I don't see the problem. Everyone else can complain and flame and talk crap about how wrong you are for not having a valid code, but when it comes down to it, it is your site and you can type whatever the hell you want into your code and there's not much anyone else can do about but refuse to visit.

It seems like the javascript honestly is the only way to do that validly though.

http://www.astahost.com/opening-window-xht...-way-t2842.html
Jetteh22
I agree.
And if somebody decides to NOT visit your site because it's not 100% XHTML valid, then screw them anywho.
MrTouz
Well its like fixing the IE bugs with the "_" using _margin:... _width.... FF does not read _ in front of the CSS codes but IE does... so you can change IE's behavior by adding _.
Tho W3C Validator does not 'validate' the code.. it is still commonly used.

I agree with you by having an invalid code by adding non important codes (non valid) to your main code such as target. But still, it is better to have a Valid code than a not valid one.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.