We know that ‘target’ is not included in XHTML 1.1 (or any of the ‘strict’ doctypes for that matter) but that leaves aspiring developers wondering: “how do I open that outside link in a new window if I can’t use ‘target’ and the link checker complains at me when I use Javascript to open those links?”

Here is the simplest solution. It’s a drop-in replacement for target=”_blank”.

onclick=”window.open(this.href); return false;”

You still must use a valid ‘href’ attribute in your <a> tag, which validates properly in a link checker, but by including the ‘return false;’ you tell the browser not to follow the link in the current window after it opens a new window. If the browser doesn’t support or the visitor has chosen to disable Javascript, then the link will function as normal, opening in the same window.

Using a bit of Javascript, you could easily turn every link on a page (or in a specific element) using the DOM

function setTargets(parent)
{
    var i;
    tags = parent.getElementsByTagName(“a”);
    for (i = 0, j = 0; i < tags.Length; i++)
    tags[i].setAttribute(‘onclick’,'window.open(this.href); return false;’);
}

Call this by passing a reference to the parent object when the page has loaded. Say you have your content in a div with an id=”content”:

window.onload = setTargets(document.getElementById(‘content’));