I use $(this).attr('href') in JQuery to read the string in href=”” attribute.
I have those kind of links:
<a href="1"></a>
<a href="2"></a>
<a href="3"></a>
Firefox and Chrome return me the code correctly. IE return me: http://127.0.0.1/1
How can i do?
You don’t need to use the
attrfunction when you’re accessing a native property. You could use the anchor element’s properties directly to get the pathname (i.e. the HREF without the domain or query string)Basically:
this.pathnameThere’s a bit of an inconsistency between browsers (some will show a leading forward slash in
pathnameand others won’t). To get around this, just get rid of any potential leading slashes:this.pathname.replace(/^\//,'')A working example: http://jsfiddle.net/jonathon/3ET6p/
Even if you choose the other answers, I recommend you use the native
.hrefon the object.Just as an extra note to this. I fired up a VM of IE6 and all is well 🙂