a tags and xhtml strict

For those of you out there who are trying to keep up with web coding standards may have noticed the ‘target’ attribute for the anchor (a) tag is not supported in xhtml strict. Now I do realize that xhtml has been a standard for some time now (2000ish), but it has only been a few months now that I have been trying to do all of my markup according to the strict standard. Before that (and ever since I knew what a DocType was) I just tried to write my code transitional compliant.

Anyway I guess they have depreciated the ‘target’ attribute (unless you are using frameset) with the idea that the World Wide Web Community has matured to the point that individual users should be able to direct the loading of hyperlinks to the window of their choice (i.e. new tabs, new windows, etc.).

I however, when I realized that the ‘target’ attribute would not validate, I decided to find a way to pull off opening a link in a new window that would not break pages.

Here is what I came up with:

<a href="go.html" onclick="return !window.open(this.href);">Click Me</a>

It requires JavaScript to work but it degrades just fine. If the popup works it will return true and then cause the onclick event to return false and stop the current window from loading the ‘href’. If the popup window fails but JavaScript is enabled on the browser (i.e. the window.open gets stopped by a popup blocker) it will return false and the onclick event will come back true and the page will be loaded into the current window as if the popup was never attempted. And finally if JavaScript is disabled the link will just function normally.

Leave a Reply

{
}