corvine74
8/17/2015 - 1:05 PM

add target="_blank" with pure javascript

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>