Skip to content

Instantly share code, notes, and snippets.

@techies23
Forked from CrocoDillon/_blank.js
Created December 3, 2018 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techies23/c8c1edb28dae39f563d1395ecc14efab to your computer and use it in GitHub Desktop.
Save techies23/c8c1edb28dae39f563d1395ecc14efab to your computer and use it in GitHub Desktop.
Automatically open external links in new tab or window.
// vanilla JavaScript
var links = document.links;
for (var i = 0, linksLength = links.length; i < linksLength; i++) {
if (links[i].hostname != window.location.hostname) {
links[i].target = '_blank';
}
}
// or in jQuery
$(document.links).filter(function() {
return this.hostname != window.location.hostname;
}).attr('target', '_blank');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment