XHTML Strict target resolved with jQuery
All good web developers now design sites using the XHTML Strict DTD. Strict XHTML forbids the use of the target="_blank" attribute on anchors, used to open a link in anew browser window. The reason for this is that there should be a separation between presentation and behaviour.
This is all fine and dandy until your customer requests that all links to external sites open in a new browser window. This is not an unreasonable request.
The answer is to add an an event to such links using Javascript. As a fan of jQuery, this could not be easier.
Step 1. Add a rel attribute to all external links/anchors
e.g. href="externalsite.com" rel="external"
Step 2. Include the following jQuery script
$('a[rel=external]‘).attr(’target’, ‘_blank’);
If adding the target attribute seems like a hack, then the following should achieve the same goal
$('a[rel=external]‘).click(function(){window.open(this.href); return false});
This may all seem a bit obvious, but I’m sure there’s someone out there looking for an elegant fix like this.