473,480 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

3 New Member
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 6648
gits
5,390 Recognized Expert Moderator Expert
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
annaGold
3 New Member
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 Recognized Expert Moderator Expert
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
annaGold
3 New Member
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 Recognized Expert Moderator Expert
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

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

Similar topics

2
9241
by: Konrad Mathieu | last post by:
Does this work in most browsers, namely MSIE? document.links.href or does it have to be document.links.href ?
6
15874
by: Carla | last post by:
hi people, I have a little problem that I can't solve with css and i was wondering if you could help me. I have 4 links, I want that when I click/mouseover in the link 1, it turns to a color...
1
1988
by: Julius Mong | last post by:
Dear all, according to the ASV3 manual known problem section, to make an <a> execute some Javascript onclick: ...
10
5342
by: andreister | last post by:
He there! I've discovered that the ================================================= document.links('link_id_here').href = "something"; ================================================= is...
31
4068
by: Greg Scharlemann | last post by:
Given some recent success on a simple form validation (mainly due to the kind folks in this forum), I've tried to tackle something a bit more difficult. I'm pulling data down from a database and...
7
1469
manuelgk
by: manuelgk | last post by:
I have another problem. I want to catch the click event, I put certain code and it works well with my code but with a little problem, I already catch the click event and the links too, but when I try...
2
2411
beacon
by: beacon | last post by:
I'm having trouble with a personal web page that I'm designing for work. I'm not well versed in HTML and could use some assistance. Currently, when I open the browser, the table in the head at the...
6
18194
by: mistral | last post by:
what is correct way open a PDF document in new window use hyperlink? I want show images thumbnails linked with PDF files, when click on thumbnail, PDF will be opened in new window. Some of PDF...
2
2191
coolv
by: coolv | last post by:
Hello I have problem in my page that the dropdown box is not displaying data according to selection of first dropdown.Please help me. Below is my code. thanks.............. <?php ...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.