d
Amit DhamuSoftware Engineer
 

Open External Links In New Tab

1 minute read 00000 views

Assuming all external links for your website start with http, with jQuery, we can target all anchor tags that start with http and force it to open in a new tab or window by dynamically adding a target="_blank" attribute to the anchor tag.

$("a[href^='http']").attr('target', '_blank')
$('a[href^="' + window.location.origin + '"]').attr('target', '_self')