2010
Oct
03

getAttribute("HREF") is always absolute

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(IE6/IE7) return the derived absolute URL.

Microsoft has tried to address this by problem adding a second parameter to the getAttribute method. The second parameter can be set to 0,1 or 2. If the parameter is set to 2 the method returns the attribute text. Any other setting will return the derived absolute URL.

Brower Attribute HREF Derived Absolute Url Attribute Text
IE linkobj.href; Yes
IE linkobj.getAttribute(‘href’); Yes
IE linkobj.getAttribute(‘href’,2); Yes
Gecko linkobj.href; Yes
Gecko linkobj.getAttribute(‘href’); Yes
Gecko linkobj.getAttribute(‘href’,2); Yes
Opera linkobj.href; Yes
Opera linkobj.getAttribute(‘href’); Yes
Opera linkobj.getAttribute(‘href’,2); Yes

View Demo

Related Posts


回應 (Leave a comment)