Created
January 19, 2016 22:58
-
-
Save rwagner00/58c9b680385dc93bb319 to your computer and use it in GitHub Desktop.
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
var links = document.getElementsByTagName("a"); | |
var link_count = links.length; | |
var checkDomain = function(url) { | |
if ( url.indexOf('//') === 0 ) { url = location.protocol + url; } | |
return url.toLowerCase().replace(/([a-z])?:\/\//,'$1').split('/')[0]; | |
}; | |
var isExternal = function(url) { | |
return ( ( url.indexOf(':') > -1 || url.indexOf('//') > -1 ) && checkDomain(location.href) !== checkDomain(url) ); | |
}; | |
for (var i = 0; i < link_count; i++) { | |
if (isExternal(links[i].href)) { | |
links[i].setAttribute('target', '_blank'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment