Created
January 22, 2020 13:39
-
-
Save dacodmagagula/f3c15a9426342539908554c5115ff659 to your computer and use it in GitHub Desktop.
Automatically Open External Links In a new tab with Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = function(){ | |
var anchors = document.getElementsByTagName('a'); | |
for (var i=0; i<anchors.length; i++){ | |
console.log(window.location.hostname); | |
if(!((anchors[i].href.indexOf(window.location.hostname) >= 0) || anchors[i].href=="#" || anchors[i].href=="javascript:void(0)" || anchors[i].href[0]=="/" || anchors[i].href[0]==".")){ | |
anchors[i].setAttribute('target', '_blank'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment