Javascript .pathname IE 怪癖?

标签 javascript internet-explorer

考虑以下 Javascript:

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

当我在包含格式为“http://foo.com/bar”的链接的页面上运行它时,在 IE8 中我得到一个看起来像“bar”的字符串。在 Safari、Chrome、Firefox 中,我返回类似“/bar”的内容(注意前面的正斜杠)。

这里是 IE 的问题,怎么回事?

最佳答案

W3C standard on the window object - 包括位置界面 - 日期为 2006 年 4 月 7 日,即它是在实际实现多年后指定的。

标准如下:

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 (#).

这意味着应该包含前导斜杠,这与 Mozilla's implementation 一致.

MSDN doc on the location object没有提到该属性包含什么,而是一个 page on the VBScript location object有一个与你的发现一致的例子。

正如所说的页面是 ©1996 - W3C 介入的十年前 - 说 IE 有错是不公平的,但我仍然认为它是一个错误。

关于Javascript .pathname IE 怪癖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/956233/

相关文章:

javascript - 根据数据动态选择指令

javascript - 更改为使用自定义下拉列表后无法看到父变量?

html - 如何解决 Joomla 站点在 Chrome/IE 和 Firefox 中的数字位置问题

javascript - Fancybox不会向YouTube显示没有class-Tag的图像

javascript - 如何测试带有 <Router> 标签的组件?

javascript - jQuery :contains works in FF, Safari 和 Chrome,而不是 Internet Explorer

internet-explorer - Internet Explorer中的XPath及其与Chrome,Firefox中Xpaths的区别

internet-explorer - Bootstrap 3.0 和 IE 8 冲突

python - 如何分析打开特定网页时的 IE 事件

javascript - 不同浏览器的 HTML