(function() {
  // Cross-browser method to add an onload event listener
  function addLoader(f) {
    // setTimeout(retarget_links, 0);
    if (window.addEventListener) {
      window.addEventListener('load', retarget_links, false);
    } else if (document.addEventListener) {
      document.addEventListener('load', retarget_links, false);
    } else if (window.attachEvent) {
      window.attachEvent('onload', retarget_links);
    }
  }

  // Find all external links and target them to open in a new window/tab
  function retarget_links () {
    var hostre = new RegExp('http://' + location.hostname, 'i');
    var links = document.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
      var link = links[i];
      if (!link.target && /^http/i.test(link.href) && !hostre.test(link.href)) {
	// Set open in a new window
        link.target = '_blank';
      }
    }
  }

  addLoader(retarget_links);
})()

