how do I make this case-insensitive

W

windandwaves

Hi Folk

I have the following function:

function eli() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("A");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors;
if (anchor.getAttribute("HREF") && anchor.getAttribute("rel") ==
"external") {
anchor.target = "_blank";
}
}
}

I run this function here : <body onload="eli();">. It adds the
target="_blank" attribute to all links that are written as follows:

<A HREF="http://www.somefruitcakesite.com" rel="external">my link</a>

How do I make it case insensitive. So that it also picks up:

<A href="http://www.somefruitcakesite.com" rel="EXTERNAL">my link</a>
<A href="http://www.somefruitcakesite.com" REL="external">my link</a>

etc...

Or is that too much work and should I adjust my html.

TIA
 
J

Joshie Surber

How do I make it case insensitive?

Change
if (anchor.getAttribute("HREF") && anchor.getAttribute("rel") == "external") {
To

if (anchor.getAttribute("HREF") && ( anchor.getAttribute("rel").toLowerCase == "external" || anchor.getAttribute("REL").toLowerCase == "external" ) ) {

This should do the trick. You may want to put in the same || for your
HREF too, since if you are serving this page as xml+xhtml (or use a
XHTML DOCTYPE in some browsers) all attributes are case sensitive.

<opinion about="standards" intent="friendly advice">On the other hand,
while this will make your code work, in reality you should probabally
standardize on using all lowercase -- if you are using XHTML lowercase
is required and if not, when you wind up eventually upgrading you won't
have as much maintenance to do. HTML Tidy (http://tidy.sf.net) can
automate this for you on your attribute names, but the values would be
up to you.</opinion>
 
B

BootNic

windandwaves said:
Hi Folk

I have the following function:

function eli() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("A");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors;
if (anchor.getAttribute("HREF") && anchor.getAttribute("rel") ==
"external") {
anchor.target = "_blank";
}
}
}

I run this function here : <body onload="eli();">. It adds the
target="_blank" attribute to all links that are written as follows:

<A HREF="http://www.somefruitcakesite.com" rel="external">my
link</a>

How do I make it case insensitive. So that it also picks up:

<A href="http://www.somefruitcakesite.com" rel="EXTERNAL">my
link</a> <A href="http://www.somefruitcakesite.com"
REL="external">my link</a>

etc...

Or is that too much work and should I adjust my html.



Adjustments would be a good ideal.

<script type="text/javascript">
function eli() {
var i,j,elix;
for (i=0, j=document.links.length; i<j; i++) {
elix=document.links;
if(elix.href && elix.rel.match(/external/i)){
elix.target="_blank";
}}}
</script>

--
BootNic Saturday, April 22, 2006 11:52 PM

"This is all very interesting, and I daresay you already see me
frothing at the mouth in a fit; but no, I am not; I am just winking
happy thoughts into a little tiddle cup."
*Nabokov, Lolita*
 
R

RobG

----------------------^^ --------------------------^^

getLowerCase is a method, use:

if (...toLowerCase() == "external" ...toLowerCase()...
 
W

windandwaves

Joshie said:
This should do the trick. You may want to put in the same || for your
HREF too, since if you are serving this page as xml+xhtml (or use a
XHTML DOCTYPE in some browsers) all attributes are case sensitive.

<opinion about="standards" intent="friendly advice">On the other hand,
while this will make your code work, in reality you should probabally
standardize on using all lowercase -- if you are using XHTML lowercase
is required and if not, when you wind up eventually upgrading you
won't have as much maintenance to do. HTML Tidy (http://tidy.sf.net)
can automate this for you on your attribute names, but the values
would be up to you.</opinion>

Totally agree, I will actually do this, but it is a huge site:
www.friars.co.nz and it takes time to go through all the code (PHP) and
adjust (well, about four hours I estimate).

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top