Quantcast
Channel: Modo Web Design » Nerd Talk
Viewing all articles
Browse latest Browse all 2

Javascript snippet: make all your site’s external links open in a new tab/browser

$
0
0

I created this little piece of code for Bay Nature so that all the externals links open in a new tab or browser, but internal links just redirect in the window.  Kind of handy. I also have it in a gist.

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;

The post Javascript snippet: make all your site’s external links open in a new tab/browser appeared first on Modo Web Design.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images