Skip to content

Instantly share code, notes, and snippets.

@corvine74
Created August 17, 2015 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corvine74/1661b31d9e1649fec08c to your computer and use it in GitHub Desktop.
Save corvine74/1661b31d9e1649fec08c to your computer and use it in GitHub Desktop.
add target="_blank" with pure javascript
<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