I know very little javascript. Nonetheless, I found some example code that opens links into a new window if they contain "external" on the "rel attribute. For example:
I later hacked the code so it works it "nofollow" is added to the "rel" attribute like:
The following is the contents on my original javascript called external.js
Now I want to use a regular expression to match different possibilities of the "rel" attribute.
I found a good tutorial on javascript regexp at:
Javascript RegExp tutorial
However no hacks I have tried based on the tutorial have worked.
I have tried the following:
and
Can anybody help!
Code:
<a href="http:example.com" rel="external">this link opens in a external window</a>
Code:
<a href="http:example.com" rel="external nofollow">this link opens in a external window with nofollow</a>
Code:
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 nofollow") || (anchor.getAttribute("rel") == "external")))
anchor.target = "_blank";
}
}
window.onload = externalLinks;
I found a good tutorial on javascript regexp at:
Javascript RegExp tutorial
However no hacks I have tried based on the tutorial have worked.
I have tried the following:
Code:
(anchor.getAttribute("href") && /(.)*(external)(.)*/i.test(anchor.getAttribute("rel")) )
Code:
(anchor.getAttribute("href") && anchor.getAttribute("rel") == /(.)*(external)(.)*/i)
Can anybody help!
Comment