Open a New Window with rel="external nofollow"

Do you use rel="external" on your webpages so links will open in a new browser window? Well, your external.js code isn't going to work for you if you ever decide to use rel="external nofollow."

In other words, the link no longer opens in a new window! What to do? Replace your current external.js file with the code below. Now your external links will, once again, open in a new browser window.

PS: The "nofollow" parameter tells Google and other search engines not to credit any PageRank to the external link. The "external" parameter tells the browser to open the link in a new window.

function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}
window.onload = externalLinks;
function externalLinks() {
if (!document.getElementsByTagName) {
return;
}
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("rel")) {
var rel = anchor.getAttribute("rel");
var external = false;
if (rel.indexOf(" ") > 0) {
while (rel.indexOf(" ") > 0 && external == false) {
if (rel.substr(0, rel.indexOf(" ")) == "external") {
external = true;
}
rel = rel.substr(rel.indexOf(" ") + 1, rel.length - rel.indexOf(" ") + 1);
}
}
if (rel == "external") {
external = true;
}
if (anchor.getAttribute("href") && external == true) {
anchor.target = "_blank";
}
}
}
return;
}


If this post has been helpful, please consider linking to our blog or sharing!
Bookmark and Share


4 comments:

Tom Breckenridge said...

YES YES!! Thanks for this post.

Anonymous said...

I added nofollow to all my rel= statements and didn't realize for a while that my external linx weren't opening in a new window anymore. This is what I needed. Thanks Patricia!

Anonymous said...

Thanks for this!

I still want to use the standard 'external.js' for links that I am willing to vouch for.
Any chance you could provide code that would allow for this.
Maybe something that uses "externalnf" for the REL attribute??
Is that even possible?? Would that even work??

Keynote said...

There are about 20 values you can use for the REL attribute: external and nofollow are 2 of them, stylesheet is another most of us use. I don't think you can make them up.

If you just want the 'new window' portion of the code and don't mind passing on the link juice, use the code above but stop at the first window.onload = externalLinks;

(November 21, 2010 7:33 PM)

Post a Comment