Skip to content

Instantly share code, notes, and snippets.

@bitzip
Forked from anselmh/target_blank--external.js
Last active August 29, 2015 14:05
Show Gist options
  • Save bitzip/2dcb45ea4085fbdfe10a to your computer and use it in GitHub Desktop.
Save bitzip/2dcb45ea4085fbdfe10a to your computer and use it in GitHub Desktop.
/**
* Open external links in new tab/window
*/
// All http:// links should open in new tab/window. Internal links are relative.
var anchors = document.getElementsByTagName('a');
for (var i = 0; i < anchors.length; i++) {
anchor = anchors[i];
if (anchor.host !== window.location.host &&
!anchor.hasAttribute('no-external')) {
anchor.setAttribute('target', '_blank');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment