Change href with @ in anchor text

D

delimiter

Hi,
I've just found a really peculiar problem with IE (v 6 and 7) and am
hoping for some help!
I have a javascript function (called immediately before the closing
body tag) that gets all the anchors in a specified div and then will
either update the href or add an onclick event depending on whether
the href is currently set to pdf. If the anchor text contains the @
symbol and there is text preceding the @ then the anchor text is
displayed on screen as the newly updated href. If there is no text
before the @ then it's fine.

Cut down example code below:

/// HTML ////
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function fnLinks() {
var oContent = document.getElementById("content");
if(oContent != null) {
var oATags = oContent.getElementsByTagName("A");
for (i=0; i<oATags.length; i++) {
oATags.href= "http://www.google.com";
}
}
}
</script>
</head>
<body>
<div id="content">
<p><a href="test1.html">Test 1</a></p>
<p><a href="test2.html">@test2.com</a></p>
<p><a href="test3.html">[email protected]</a></p>
<p><a href="test4.html">@test4.com</a></p>
<p><a href="test5.html">[email protected]</a></p>
</div>
<script type="text/javascript">fnLinks();</script>
</body>
</html>

Many thanks
Helen
 
B

Botan Guner

Hi,

You can use indexOf to detemine the position of @ like;

for (i=0; i<oATags.length; i++) {
if( (oATags.href.indexOf("@")!="-1") ||
(oATags.href.indexOf("@")!="0") {//-1 means not found in string 0
means at the begining
oATags.href= "http://www.google.com";
}
}
 
K

kindy

when text in a is 'xxx@xxx', ie treat it as a mail link; so it
synchronization the text when you modify the link's href.

if(oContent != null) {
var txt;
var oATags = oContent.getElementsByTagName("A");
for (i=0; i<oATags.length; i++) {
txt= oATags.innerHTML;
oATags.href= "http://www.google.com";
oATags.innerHTML= txt;
}
txt= null;
}
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top