Skip to content

Instantly share code, notes, and snippets.

@aahung
Created December 4, 2015 16:05
Show Gist options
  • Save aahung/b42f7e19073b81d72fc0 to your computer and use it in GitHub Desktop.
Save aahung/b42f7e19073b81d72fc0 to your computer and use it in GitHub Desktop.
Change external links' target = '_blank'
$(function(argument) {
// set target=_blank for all external links
// credit: http://stackoverflow.com/questions/2910946/test-if-links-are-external-with-jquery-javascript
var nodes = document.getElementsByTagName("a"), i = nodes.length;
var regExp = new RegExp("//" + location.host + "($|/)");
while (i--) {
var href = nodes[i].href;
var isLocal = (href.substring(0,4) === "http") ? regExp.test(href) : true;
if (!isLocal)
nodes[i].target = "_blank";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment