Created
August 17, 2015 13:05
-
-
Save corvine74/1661b31d9e1649fec08c to your computer and use it in GitHub Desktop.
add target="_blank" with pure javascript
This file contains 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
<script> | |
var anchors = document.getElementsByTagName("a"); | |
var host = new RegExp('/' + window.location.hostname + '/'); | |
for (var i=0; i < anchors.length; i++){ | |
if( !(host.test(anchors[i].href)) ){ | |
anchors[i].setAttribute("target", "_blank"); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment