Skip to content

Instantly share code, notes, and snippets.

@jpjuliao
Created September 23, 2019 02:35
Show Gist options
  • Save jpjuliao/353fa3c94a978ef211ae304489d008e1 to your computer and use it in GitHub Desktop.
Save jpjuliao/353fa3c94a978ef211ae304489d008e1 to your computer and use it in GitHub Desktop.
Open external links in new window, except mailto and tel.
function OpenExternalLinksInNewWindow() {
$('a').each(function() {
if (
this.href.startsWith('tel:')
|| this.href.startsWith('mailto:')
) {
return;
}
var a = new RegExp('/' + window.location.host + '/');
if(!a.test(this.href)) {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment