Opening a New Browser Window in a Standards Compliant World

While tidying up a project in Eclipse recently, I had an unexpected warning in a JSF XHTML file.  The root cause, while fairly trivial is still for some interest to those trying to get the last dot and cross in place.

The code generating the warning was in itself not complex.  Just a link with … wait for it! … a target=”_blank” tag.  The age old and accepted way of opening a link in a new browser window.  What could possibly be wrong with this I mused.  The answer, in one word,  is standards.  Or to be more precise, Strict standards.

The “target” tag is not defined in either HTML 4.0 or XHTML 1.0 strict.  W3C considers this functionality to be outside of the remit of the standard and well into the land of client-side script.

The solution is to remove the “target” tag and replace with a small piece of JavaScript to do the job instead.  This is where things become slightly surreal as the attribute “target” is still readily available.  This is because the standard that defines the DOM available to JavaScript is completely separate, and more importantly, defines “target”.

Before:

<a  href="http://softwaresimian.com"  target="_blank">Linky</a>

After:

<a  href="http://softwaresimian.com" 
                     onclick="this.target='_blank';">Linky</a>