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

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

P: 3
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
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,333
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

P: 3
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
Expert Mod 5K+
P: 5,333
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

P: 3
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
Expert Mod 5K+
P: 5,333
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.