473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

External links in new window..wild card regexp

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
3 2561
acoder
16,027 Expert Mod 8TB
You can use any of the String search methods, e.g. indexOf, match, search. See this reference.
Aug 10 '08 #2
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
16,027 Expert Mod 8TB
You're welcome. Glad you've got it working.
Aug 11 '08 #4

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

Similar topics

0
by: Simon Brunning | last post by:
QOTW: "Not tested but confident should be an oxymoron for a programmer." - Peter Otten (Asked "Is this unsurprising if I look at it right?") - "Yes; in general this is true across many domains...
0
by: Gerry Viator | last post by:
Hi all, I want to remove the test after a period? Why does the wild card * not work? Dim test As String = "This is just a test report. ERCP" MessageBox.Show(Replace(test, ".*", "."))
1
by: Steve T | last post by:
I have just contracted with a firm that is using a front-end web interface to access our sales data. They developed it with Microsoft Dotnet, and it uses Microsoft SQL as the database. When I...
10
by: bbcrock | last post by:
does anyone have sample code to display a pop up when a user leaves the site? I really don't want to use this technique, but the clients demand it. I'm thinking something like un body onunload,...
1
by: Tempy | last post by:
Could somebody please tell me if it is possible and if how? I am wanting to do : Iif(="*PE","Y","N") In other word i want it to fine HZPE, AEPE, HEPE etc... Les Stout *** Sent via Devdex...
15
by: Roedy Green | last post by:
I seem to recall reading about a feature so that you could apply different styles to different kinds of link. e.g. local and offsite or to automatically put a logo beside some domain links. I...
7
tpgames
by: tpgames | last post by:
I know that an internal sound card has to be one listed as for a specific mother board. However, does this rule apply to external sound cards? I use a satellite A75-s2112 toshiba laptop, intel pent...
0
by: =?Utf-8?B?c25naWxi?= | last post by:
I am having 3 issues with the WebBrowser control which may all be related. The HTML for the page is the standard Weather Magnet from weather.com. The actual HTML is at the bottom of this page. ...
7
by: W. eWatson | last post by:
Is it possible to do a search for a wild card string in another string. For example, I'd like to find "v*.dat" in a string called bingo. v must be matched against only the first character in bingo,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.