The Infinite Loop – Beginner's SEO, Beginner C# & JQuery Tutorials Problem. Problem Solved. Loop. – The life of a programmer

17Jun/090

Using javascript to opening a new window W3C Validation target="_blank" alternative

<a href="http://www.google.com" target="_blank">Google!</a>
Perfect way to open a new window right? WRANG! (End of the world flash reference ...look it up)

Your webpages may not pass W3C validation because your still using target="_blank".
This article will show you how to use javascript or jquery to open a new window, and have your webpages pass W3C Strict Validation.

Anyyyyyyyyyyyways, using the target attribute of the a tag was a perfectly valid way of opening a link in a new window. However, this currently does not pass certain XHTML Validations. I assume you found this blog post because you need to validate and target="_blank" isn't passing.

This solution will open your link in a new window.
<a href="http://www.google.com" onclick="window.open(this.href);return false;">Google New Window!</a>

What this does is use window.open, and passes the links href attribute as the url.
The reason for "return false", is so that the base web page does not open the link as well.

Another cool alternative is to use JQuery...ya I know this is javascript but if you have jquery already loaded you can add this to your document.ready function.

$(document).ready(function(){
$("a.newWin").click(function(){
window.open($(this).attr("href"));
return false;
});
});

<a href="http://www.google.com" class="newWin">Google</a>

Any link with the class newWin will now open in a new window.

Both these methods validate to XHTML Strict and are easy to implement.

Enjoy.