Skip to content

Instantly share code, notes, and snippets.

@bgantick
Last active December 9, 2016 20:48
Show Gist options
  • Save bgantick/9269e8a24e9423d06910be19c7f80d32 to your computer and use it in GitHub Desktop.
Save bgantick/9269e8a24e9423d06910be19c7f80d32 to your computer and use it in GitHub Desktop.
Open external links in a new tab/window
window.onload = function() {
var anchors = document.getElementsByTagName('a');
var a = new RegExp('/' + window.location.host + '/');
for (var i = 0; i < anchors.length; i++) {
if (!a.test(anchors[i])) {
anchors[i].setAttribute('target', '_blank');
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment