Monday, April 21, 2008

XHTML Target Attribute, Open New Windows With JavaScript

The XHTML Strict web standard has removed the "target=" attribute.

Here is an article about a (somewhat complex) way to handle this in a universal way with the DOM2 API and create valid XHTML that opens new windows where appropriate.

A less comprehensive approach, and the one I elected to use, was to add this to replace the "target" attribute:

onclick="this.target = '_blank';"

Is this not cheating?  We are not using the old HTML "target" attribute, but we are still adding something called "target" to our precious, and now valid, A-Href.

The answer is no. The Document Object Model (DOM), which governs the document objects and attributes that are available to JavaScript code, is a totally separate standard from (X)HTML.

Also consider that the DOM 2.0 standard that was published in January 2003 (well after XHTML 1.0, let alone HTML 4.0) still includes this attribute. It seems clear that while this attribute is slated to be phased out of (X)HTML, it will be available to JavaScript through the DOM for the foreseeable future.

No comments: