Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

internet explorer - Javascript .pathname IE quirk?

Consider the following Javascript:

var anchors = document.getElementsByTagName('a');
for(var i=0; i < anchors.length; i++)
{
    alert(a.pathname);
}

When I run this on a page that contains links in the format "http://foo.com/bar", in IE8 I get back a string that looks like "bar". In Safari, Chrome, Firefox, I get back something like "/bar" (note the leading forward slash).

Is IE at fault here, what's the deal?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The W3C standard on the window object - including the location interface - is dated 07 April 2006, ie it was specified after actual implementations had been around for years.

The standard reads:

pathname

This attribute represents the path component of the Location's URI which consists of everything after the host and port up to and excluding the first question mark (?) or hash mark (#).

This means the leading slash should be included, which is consistent with Mozilla's implementation.

The MSDN doc on the location object doesn't mention what the property contains, but a page on the VBScript location object has an example consistent with your discovery.

As said page is ?1996 - ten years before the W3C got involved - it's hardly fair to say that IE is at fault, but I'd still consider it a bug.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...