469,610 Members | 2,110 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

problem changing document.links[0].href in IE

Hello, I wrote this simple javascript to demonstrate my problem with IE6 & 7. The code works find in Mozilla, but in IE my email link on the page is changed to the new href. Only href should change, but not the link itself. What's even more interesting is that only email serving as a link on the page gets changed. If it's a word, for example, Name, it stays unchanged in IE. I hope you can help me to find IE equivalent to the document.links[].href that doesn't modify an email link. Thank you.


[HTML]<html>
<head>
<title>Title</title>
<body>
<h2>Display an Email</h2>
<a href="mailto:anna@gmail.com">name@test.com</a>
<br />
<a href="mailto:anna@test.com">Name</a>


<script language="javascript">
var prev_href = document.links[0].href;
document.links[0].href = "mailto:name@newemail.com";
alert(prev_href + " is changed to " + document.links[0].href);
</script>

</body>
</html>[/HTML]
Jan 10 '08 #1
5 6463
gits
5,390 Expert Mod 4TB
hi ...

you should use regular dom-methods for getting element-nodes and even use the onload-event to make document changes through javascript ... in onload the entire document is loaded and its dom is reliably ready to use, have a look at the following example:

[HTML]<html>
<head>
<title>Title</title>
<script type="text/javascript">
function change_link() {
var link_list = document.getElementsByTagName('a');
var lik = link_list[0];
var prev_href = lik.href;

lik.href = "mailto:name@newemail.com";
alert(prev_href + " is changed to " + lik.href);
}
</script>
</head>
<body onload="change_link();">
<h2>Display an Email</h2>
<a href="mailto:anna@gmail.com">name@test.com</a>
<br/>
<a href="mailto:anna@test.com">Name</a>
</body>
</html>
[/HTML]
kind regards
Jan 10 '08 #2
Thank you, Gits

But I tried that already and encountered the same problem: it works in Mozilla and IE changes the email link on the screen instead of changing just its href.
Jan 10 '08 #3
gits
5,390 Expert Mod 4TB
nope ... i tested it in FF 2.0.0.11 and IE 6 ... the provided example changes the href (an only the href) correctly ... post the code you tried, there must have been another problem.

kind regards
Jan 10 '08 #4
Hi Gits

Thank you again for you response!

The problem is solved

For some reason IE changes inner HTML as well, so I had to assign the innerHTML back to its prev value.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.         function change_link() {
  3.             var link_list = document.getElementsByTagName('a');
  4.             var lik       = link_list[0];
  5.             var prev_href = lik.href;
  6.             var txt = lik.innerHTML;
  7.             lik["href"] = "mailto:name@newemail.com";  
  8.             lik.innerHTML = txt;
  9.             //lik.href = "mailto:name@newemail.com";  
  10.             alert(prev_href + " is changed to " + lik.href +"["+ txt +"]");
  11.         }
  12.         </script>
I appreciate your help!
Jan 10 '08 #5
gits
5,390 Expert Mod 4TB
hi ...

i even noticed that ... silly IE-behaviour ... we could even fix it with a span:

[HTML]<a href="mailto:anna@gmail.com"><span>name@test.com</span></a>[/HTML]
i noticed that IE only changes it accordingly when there is an e-mail address as innerText in the a-tag ...

kind regards
Jan 10 '08 #6

Post your reply

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

Similar topics

2 posts views Thread by Konrad Mathieu | last post: by
6 posts views Thread by Carla | last post: by
1 post views Thread by Julius Mong | last post: by
10 posts views Thread by andreister | last post: by
31 posts views Thread by Greg Scharlemann | last post: by
beacon
2 posts views Thread by beacon | last post: by
coolv
2 posts views Thread by coolv | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.