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;
{
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.
