首页 > 编程 > HTML > 正文

英文:A链接标记ie下会自动补全href

2024-08-26 00:09:20
字体:
来源:转载
供稿:网友

whilst working on the ajax link tracker and mapsurface i have come across an inconsistency in how the href attribute is retrieved using dom scripting.

the href attribute is different to other element attributes in that the value set can be relative to the context of the page url. if you set a link with a relative href attribute

<a href="../development/test1.html">test page</a>

the browser will look at the pages current url and derive an absolute url for the link.

http://www.glenn.jones.net/development/test1.html

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. there are three common ways to access an attribute:

    linkobj.href;    linkobj[‘href’];    linkobj.getattribute(‘href’);

the linkobj.href and linkobj[‘href’]; methods of accessing the attribute consistently 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.

    linkobj.getattribute(‘href’);    linkobj.getattribute(‘href’,2);
    derived
absolute url
attribute text
ie linkobj.href; x  
ie linkobj.getattribute(‘href’); x  
ie linkobj.getattribute(‘href’,2);   x
gecko linkobj.href; x  
gecko linkobj.getattribute(‘href’);   x
gecko linkobj.getattribute(‘href’,2);   x
opera linkobj.href; x  
opera linkobj.getattribute(‘href’); x  
opera linkobj.getattribute(‘href’,2); x  
get attribute test page test on ie6, firefox 1.5 and opera 8.51.

so what should be returned by the getattribute method? the w3c dom level 2 core specification which sets out the structure of the getattribute method does not cover this issue. it is not that either approach is wrong or right. on this point the specification is open to interpretation.

as a coder i would like to be able to access both values. the dom core specification should be updated to address the problem.

after a really good exchange with jim in the comments below, i stand corrected. the specification does say the getattribute should return the attribute value, not the absolute url. the microsoft approach is wrong.

for the time being i am using the old school object property method linkobj.href to return derived absolute urls. it provides the most consistent results across all browsers.

urls of interest
w3c rec dom level 2 core specification for getattribute
gecko documentation for getattribute
microsoft documentation for getattribute

as usual just as i was finishing this post i found this bug report on the quickmode site which discusses the same subject.
getattribute href is always absolute.html
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表