Getting the href value with jQuery in IE

I ran into a ‘bug’ in jQuery that only occurs in IE and under specific circumstances. Actually, it’s not really a bug in jQuery… it’s more a funky “feature” of our beloved* browser: Internet Explorer. (* yeah right, muaha :P )

In jQuery you can easily get the href attribute value off of a link by doing $("mylink").attr("href"); but sometimes in IE this isn’t reliable. For example: normally, if the href value is “#myAnchor”, jQuery will return —surprise, surprise— the string: “#myAnchor”. But in some cases with IE, you’ll get the full URL path of the page you’re currently on, with the anchor string value appended to it (for example: “http://labs.thesedays.com/#myAnchor”).

This bug is specifically annoying when you use the href value in a selector in jQuery. For example, you should never do the following, because it may result in unexpected results when browsing in IE: $("a[href='#myAnchor']").click(...);

Workaround? Instead of using the href attribute, you could use the rel or xref attributes of a link, but these attributes aren’t supposed to be used for this purpose and if you disable javascript, the anchors won’t work anymore.

The  jQuery devs should fix this issue ASAP, because I think that issues like these are the main reason why people use javascript libraries like jQuery: they make hacky javascript work!

For me, this proves once again that Internet Explorer totally fails at DOM management.

5 comments

  1. Maybe the safest way would be to use a selector that matches hrefs that end with your anchor name… $(“a[href$=#myAnchor]“).click(…);

    Don’t you think?

    Marin | 8 Jan. 2010
  2. [...] Getting the href value with jQuery in IE « These Days Labs Tags: different-types, published-volumes, some-cases, string, term, the-intrinsic, the-page, the-string Gadget Value « The Gondola ProjectUsing a ViewModel as a value converter « Josh Smith on WPFThe value of hedge-fund experience | Analysis & Opinion | ReutersTax Research UK » Substantial destruction of value is just around …Bankruptcy Attorneys Should Not Overlook the Value of their Local …Civil Eats » Blog Archive » Added Value: Direct Marketing for …The value of boozeHow Can I Earn Money Online To Get The Money Within 2-3 Weeks …How Ordinary Folks Earn Cash Online With Affiliate Marketing …Want to earn money? Making money online with the new My Internet … View the Contact Powered by Mobile [...]

  3. [...] This post was mentioned on Twitter by Bramus! and goofypig, Michael Davis. Michael Davis said: RT @goofypig: Getting the href value with jQuery in IE « These Days Labs http://ow.ly/Ulex #jQuery #webdesign [...]

  4. Social comments and analytics for this post…

    This post was mentioned on Twitter by bramus: @jeresig Any chance you could land a fix/workaround for http://bit.ly/7I3IXm in the upcoming #jquery release?…

  5. @marin that’s a great temporary solution, but I think jQuery should “fix” it anyway ;)

    donotfold | 12 Jan. 2010

Leave a reply

Security Code:

Switch to our mobile site