Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#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:
Blocked by: Blocking:


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">
        <title>Bug report</title>
        <script type="text/javascript" src="jquery-1.5.2.js"></script>
        <a href="#"> </a>
        <script type="text/javascript">
$(window).load(function() {
    $('a').attr("href", "http://www.example.com");

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 (7)

comment:1 Changed 6 years ago by timmywil

Owner: set to patrick@…
Status: newpending

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.

comment:2 Changed 6 years ago by patrick@…

Status: pendingnew

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 6 years ago by timmywil

Component: unfiledattributes
Priority: undecidedlow
Resolution: patchwelcome
Status: newclosed

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 6 years ago by patrick@…

small update:
Affected Browser: Internet Explorer <= 8
Not affected Browser: Chrome, Safari, Firefox, Internet Explorer = 9

comment:5 Changed 6 years ago by timmywil

#9428 is a duplicate of this ticket.

comment:6 Changed 6 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.

Version 0, edited 6 years ago by dmethvin (next)

comment:7 Changed 6 years ago by timmywil

It seems it still has the same behavior when setting the property. This bug has been present since jQuery 1.0. I suggest setting the text after setting the href. The bug does not occur if text is not altered with javascript beforehand.

Note: See TracTickets for help on using tickets.