Open links in a new window with jQuery
It is possible to open links in a new window by adding target="_blank" to the anchor tag in HTML, but if your page uses the XHTML doctype it will generate a validation error. The following snippet uses JavaScript instead to open the link in a new window, and keeps your XHTML valid.
Make sure you have jQuery loaded, then include this in your JavaScript file:
|
1 2 3 4 5 |
$('a[rel="external"]') .click( function() { window.open( $(this).attr('href') ); return false; }); |
Now you just need to add rel="external" to any anchor tags that you want to open in a new window (e.g. <a href="somedomain.com" rel="external">some link text</a>).
(Update: Apparently using target="_blank" is valid in HTML5. But this JavaScript method will still work too).