Created
June 6, 2014 19:19
-
-
Save lschatzkin/41eb26dd46774a1a763f to your computer and use it in GitHub Desktop.
add target=_blank to all external links on a site
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 externalLinks() { | |
var url_test = false; | |
var anchors = document.getElementsByTagName("a"); | |
for (var i=anchors.length; i>=0; i--) { | |
var anchor = anchors[i]; | |
anchor_url = encodeURI(anchor); | |
url_test = /baynature/.test(anchor_url); | |
// baynature is the name of the site | |
if (url_test == false){ | |
if (anchor_url != "undefined"){ | |
//not sure where the undefined was coming from, but showed up as first anchor tag on site | |
anchor.target = "_blank"; | |
} | |
} | |
} | |
} | |
window.onload = externalLinks; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment