By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

External links in new window..wild card regexp

P: 3
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:
Expand|Select|Wrap|Line Numbers
  1. <a href="http:example.com" rel="external">this link opens in a external window</a>
I later hacked the code so it works it "nofollow" is added to the "rel" attribute like:
Expand|Select|Wrap|Line Numbers
  1. <a href="http:example.com" rel="external nofollow">this link opens in a external window with nofollow</a>
The following is the contents on my original javascript called external.js
Expand|Select|Wrap|Line Numbers
  1. function externalLinks() {
  2.  if (!document.getElementsByTagName) return;
  3.  var anchors = document.getElementsByTagName("a");
  4.  for (var i=0; i<anchors.length; i++) {
  5.    var anchor = anchors[i];
  6.    if (anchor.getAttribute("href") &&
  7.        ((anchor.getAttribute("rel") == "external nofollow") || (anchor.getAttribute("rel") == "external")))
  8.  
  9.  
  10.      anchor.target = "_blank";
  11.  }
  12. }
  13. window.onload = externalLinks;
  14.  
  15.  
  16.  
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:
Expand|Select|Wrap|Line Numbers
  1. (anchor.getAttribute("href") &&  /(.)*(external)(.)*/i.test(anchor.getAttribute("rel")) )
and
Expand|Select|Wrap|Line Numbers
  1. (anchor.getAttribute("href") &&  anchor.getAttribute("rel") == /(.)*(external)(.)*/i)


Can anybody help!
Aug 9 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
You can use any of the String search methods, e.g. indexOf, match, search. See this reference.
Aug 10 '08 #2

P: 3
You can use any of the String search methods, e.g. indexOf, match, search. See this reference.
Thanks Acoder!

IndexOf worked like a charm!
BTW here is my new script:

Expand|Select|Wrap|Line Numbers
  1. function externalLinks() {
  2.  if (!document.getElementsByTagName) return;
  3.  var anchors = document.getElementsByTagName("a");
  4.  for (var i=0; i<anchors.length; i++) {
  5.    var anchor = anchors[i];
  6.    if (anchor.getAttribute("href") && anchor.getAttribute("rel").indexOf("external") != -1)
  7.  
  8.  
  9.      anchor.target = "_blank";
  10.  }
  11. }
  12. window.onload = externalLinks;
Aug 10 '08 #3

acoder
Expert Mod 15k+
P: 16,027
You're welcome. Glad you've got it working.
Aug 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.