Created
December 4, 2015 16:05
-
-
Save aahung/b42f7e19073b81d72fc0 to your computer and use it in GitHub Desktop.
Change external links' target = '_blank'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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