Thursday, May 25th, 2006

getAttribute href bug in DOM

Category: JavaScript, Programming

Glen Jones has discovered a little “gotcha” while he was wokring on one of his scripts – an issue with the retrevial of the href attribute when working with the DOM.

The href attribute is different to other element attributes in that the value set can be relative to the context of the page URL. The browser will look at the pages current URL and derive an absolute URL for the link. This is the root of the problem, some browsers return the text of the attribute and others return the derived absolute URL. The results also differ by the method you use to retrieve the href attribute.

He gives the results of a few tests he performed (with IE and Gecko) to see underwhat circumstances it would return the correct attribute text and when it would make it relative. He had made some comments about which way was the “more correct” way, but has since changed them (because of some comments) and noted that the way IE tends to handle it is incorrect.

Posted by Chris Cornutt at 6:30 am
4 Comments

++++-
4.2 rating from 18 votes

4 Comments »

Comments feed TrackBack URI

Chris: You spell like a programmer.

Comment by jaredmellentine — May 25, 2006

Yeah, I just stumbled across this on a recent project. Expecting the DOM to deliver the actual text of the attribute, I kept getting errors and scratching my head. Dig a little deeper and voila! It’s returning an absolute URL relative to the current page! Since I was concatenating the result with an absolute path, it was doubling up on me. All the while, I’m staring at the Gecko DOM reference going, “huh?”

Thanks for the link… now I know what to expect from which browser. One more cheatsheet for my arsenal.

Comment by Bradley — May 25, 2006

Yeah, i actaully spend some time staring at it too. If you want to do tag replacing, and need that relative path you better use title attribute.

Comment by Mark — May 26, 2006

Doesn’t seem like a bug to me. Isn’t that what linkobj.pathname, linkobj.search, and linkobj.hash are for? One more inconsistency…

Comment by Luke — May 26, 2006

Leave a comment

You must be logged in to post a comment.