Ticket #8805 (closed bug: patchwelcome)
Internet Explorer changes link text when changing the href
| Reported by: | patrick@… | Owned by: | patrick@… |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | attributes | Version: | 1.5.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
Today I came across a IE bug and wanted to ask if we could apply a generic workaround to jquery. Here is the snipped which cases Internet Explorer to change the text when changing a href attribute:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Bug report</title>
<script type="text/javascript" src="jquery-1.5.2.js"></script>
</head>
<body>
<a href="#"> </a>
<script type="text/javascript">
$(window).load(function() {
$('a').text("www.example.org");
$('a').attr("href", "http://www.example.com");
});
</script>
</body>
</html>
The text node should be www.example.org, but is "www.example.com".
My workaround: set the href first, then set the text node, but that is not a generic solution! generic workaround is explained at <a href=" http://stackoverflow.com/questions/1790698">stackoverflow</a>. Temp. store the text node and restore it after setting the href attribute.
Change History
comment:2 Changed 2 years ago by patrick@…
- Status changed from pending to new
Sorry, here is the jsFiddle.
Affected jQuery versions: 1.4.2 - 1.5.2
Affected Browser: Internet Explorer
Not affected Browser: Chrome, Safari, Firefox
Affected operating system: Windows
HowTo reproduce the issue: open jsFiddle in Internet Explorer
Expect to happen: result in jsFiddle should be
<a id="testNode" href="http://www.example.com">www.example.org</a>
but in Internet Explorer it is:
<a id="testNode" href="http://www.example.com">http://www.example.com</a>
comment:3 Changed 2 years ago by timmywil
- Priority changed from undecided to low
- Resolution set to patchwelcome
- Status changed from new to closed
- Component changed from unfiled to attributes
Confirmed. I've looked into fixing this and I'm not sure it is worth the amount of code this requires. I tried setting the attribute every way I could think of and if I'm right, it would mean another test in support.js and at least 6-7 lines in attributes.js because it would mean a setter hook for href that temporarily saves out the anchors contents then reinserts them after setting the href.
IE changes the text only in certain cases. When setting the href to "www.jquery.com", I could reproduce the bug with the text set to "www.jquery.org", but not when set to "jquery". This is admittedly odd and frustrating behavior, but you probably don't want to set the text to .org when the href is going to .com. I suggest setting the text after the href for now and if someone can think of a way to add a fix for this without inflating the code too much, I think we would welcome a patch.
comment:4 Changed 2 years ago by patrick@…
small update:
Affected Browser: Internet Explorer <= 8
Not affected Browser: Chrome, Safari, Firefox, Internet Explorer = 9
comment:6 Changed 2 years ago by dmethvin
Hmmm, IE<9 only seems to misbehave if the attribute is set, and not the property. So one workaround is to use:
$("#whatever").prop("href", "http://example.com/");
As long as you don't plan to serialize the HTML of the link (via .innerHTML or .clone()) there shouldn't be any negative impacts.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced test case on http://jsfiddle.net to help us assess your ticket. I'm not sure I understand what the problem is here.