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:

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).

Leave a Reply