Pages

Sunday, June 20, 2010

target="_blank" gone?

Recently I had remembered reading somewhere, or had learned in school, that the HTML attribute target isn't supported in the strict doctype for the latest version of HTML. Or in the least the _blank value wouldn't. Which would absolutely suck if true. After some research and found out it was true for HTML 4.01/XHTML 1. Which isn't really new. (I first heard about this years ago, when I was just learning about web design.)

So for the fun of it and for practice I searched online to find a work around and then implement it in this blog.

First I found a forum post saying how it can be done. It then linked to a site point article that goes into more detail. Basically you put a little bit of JavaSctipt into your page that will put target="_blank" if there are any anchor tags that have rel="external.

Wait wait... why go through all of that, to put the very thing your trying to avoid into the tag? If you had the attribute and value target="_blank" in the first place. the site won't validate against HTML 4/XHTML 1. But putting it in after the fact is A-OK. Because its allowable in DOM. Crazy uh? Here is the 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;



Wile trying to implement this I found out you can't put JavaScript into Blogger templates. Since the templates are done in xml you have to wrap CDATA around JavaScript.

So I do that then go through a previous post and replace target="_blank" with rel="external". Test it and see that it works. Awesome. Doing it to the others wasn't too hard, I copy and pasted the posts into Notepad++ then used it's Find and Replace all feature. It was just tedious.

Looking more into HTML I see that HTML5 is in development (has been for about 2 or so years). According to w3Schools' html5 pages, and if I read w3.org right, target="_blank" is coming back. But it isn't scheduled to be done till about 2012.

After all of that I then removed the code and put the target attribute back in. Which seems like a waste of perfectly good time ( I'm really good at that by the way). Also for the fact that it's coming in verrsion 5. But this was a good learning experience.

No comments: