窗口之间有什么区别。Location和document。Location ?它们是否都引用同一个对象?


当前回答

Document.location.constructor === window.location.constructor为true。

这是因为它与document.location===window.location中看到的对象完全相同。

因此,不需要比较构造函数或任何其他属性。

其他回答

是的,它们是一样的。这是浏览器JS API的众多历史怪癖之一。试着做:

window.location === document.location

文档。location最初是一个只读属性,尽管Gecko浏览器也允许你为它赋值。为了跨浏览器安全,请使用window。位置相反。

阅读更多:

document.location

window.location

窗口。考虑到较老的浏览器,位置是两者中更可靠的一致性。

文档。Location ===窗口。Location返回true

also

Document.location.constructor === window.location.constructor为true

注:仅在Firefox 3.6、Opera 10和IE6上测试

我想说是窗户。location是更可靠的获取当前URL的方式。 下面是窗口的区别。位置和文档。在其中一个场景中,我在url中添加哈希参数,然后读取它。

在URL中添加散列参数后。

在较旧的浏览器中,我无法通过使用document从URL获得散列参数。url,但当我使用窗口。然后我就能从URL中得到哈希参数。

所以最好使用window。location。