Link Targets in XHTML
I always prefer to build websites in valid XHTML as it allows for a stricter, more maintainable envorinment to build websites than traditional HTML. However, there are a few features of HTML I like to make use of.
For example, making links open in new windows using the target attribute is unsupported by XHTML.
There are two solutions I know of to get around this:
1. JavaScript
The DOM specification allows the use of the target attrbriute. So, mark each link you want to open in a new window using rel="external". When the page loads use JavaScript to find all these links and add a target="_blank" to each using DOM.
In MooTools the code would look like:
window.addEvent('domready', function() {
// rel="external" links open in new window
$$('a[href][rel="external"]').each(function(anchor) {
anchor.setProperty('target', '_blank');
});
});
For a raw JavaScript solution and more information on this approach see this Sitepoint article.
2. Extending XHTML
Simply extend XHTML to support the target attribute and continue using target="_blank" whilst still producing perfectly valid XHTML!
Essentially you extend the default XHTML DTD to enable the XHTML Target Module (which is not enabled by default) and then modify the page DOCTYPE definition to point to the extended DTD.
The full details and how to implement it can be found here at TexaStar.
Currently I am using the JavaScript workaround. However, modifying the XHTML definition to make it work for me is a very elegant solution that I would look to implement in the future.
Posted by Daniel Skinner 2008-03-15 13:33:56
Categories: Guides, Web Development
Tags: javascript, mootools, xhtml



