Skip to content

Instantly share code, notes, and snippets.

@kylefox
Forked from bastianallgeier/external.js
Last active February 6, 2016 16:33
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 kylefox/63bfd3ba2d1d0d1a5bbb to your computer and use it in GitHub Desktop.
Save kylefox/63bfd3ba2d1d0d1a5bbb to your computer and use it in GitHub Desktop.
For those "external links should open in new tabs" clients…
// Array.forEach potentially faster than loop
// http://jsperf.com/testing-foreach-vs-for-loop
var links = document.querySelectorAll('a[href*="//' + window.location.host +'"]');
[].slice.call(links).forEach(function(a) {
a.target = '_blank';
});
var a = document.querySelectorAll('a[href*="//' + window.location.host +'"]'),
length = a.length;
for(var i = 0; i < length; ++i) {
a[i].target = '_blank';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment