Open all external links in a new window

var externalLinks = document.querySelectorAll('a');

function handleExternalLinks(e) {
  e.preventDefault();
  window.open(this.href);
}

externalLinks.forEach(function(externalLink) {
  if(externalLink.host !== location.host) {
    externalLink.addEventListener('click', handleExternalLinks);
  }
});
#1237
3 weeks ago