This post originated from an RSS feed registered with Web Buzz
by Douglas Clifton.
Original Post: Serving "Valid" XHTML Strict with target="_blank"
Feed Title: blogZero
Feed URL: http://loadaveragezero.com/app/s9y/index.php?/feeds/index.rss1
Feed Description: Web Development News, Culture and Opinion
The target attribute is designed for frames (who uses frames?)—if you want to use targets, use a Frameset or Transitional DOCTYPE.
Sadly, I am witnessing an ever growing trend with sites that serve strict document types (XHTML in particular) and insist on opening new windows from external links. Yet they still want to sport the "Valid" XHTML W3C badge on their sites.
The trick? Simple, just use a rel attribute of "external" on those anchor elements, and then dynamically alter the DOM using JavaScript to add a "_blank" target attribute.
It took me all of 5 minutes to write this little function and test it with an XHTML 1.1 document:
/* give anchors with attribute rel="external" a target attribute of "_blank" */
function external() {
if (document.getElementsByTagName) {
var i, a;
a = document.getElementsByTagName('a');
for (i in a) {
if (a[i].getAttribute('href') &&
a[i].getAttribute('rel') == 'external') a[i].target = '_blank';
}
}
}
window.onload = external;
/* external.js */
The results? The W3C Validator happily reported "This Page Is Valid XHTML 1.1!" Why? Because of course it doesn't know anything about what happens to the DOM after you've altered it with client-side scripting.
Is this old news? Probably. Do I intensely dislike people who insist on opening new browser windows just because they are under the (false) impression they will somehow retain the visitor by doing so? Yes, very much. And I happen to the think this point of view is ass backwards. Thankfully, I can use a browser like Firefox that allows me to at least redirect these links to a tab—and when I notice it happening, it usually only takes me one more visit before I mentally blacklist that site forever.
Are there times when opening a new window is warranted? Occasionally, if done with care and for the right reasons. I'm not going to get into that. Argue amongst yourselves.