Set a link to open in a new window / tab with jQuery

Ideally you’d set the link to open in a new window or tab directly in the HTML code itself, however you might not always have access to that.

This piece of code will add the target=”_blank” attribute to your link with jQuery instead, so when the link is clicked / tapped it opens in a new tab or window (determined by the browser settings, but most browsers these days will default to a new tab).

[gist id=91eb2a338d4954b0f02a]

(You can of course argue the pros and cons of ‘forcing’ a link to open in a new window or tab).