窗口之间有什么区别。Location和document。Location ?它们是否都引用同一个对象?
当前回答
是的,它们是一样的。这是浏览器JS API的众多历史怪癖之一。试着做:
window.location === document.location
其他回答
是的,它们是一样的。这是浏览器JS API的众多历史怪癖之一。试着做:
window.location === document.location
Document.location.constructor === window.location.constructor为true。
这是因为它与document.location===window.location中看到的对象完全相同。
因此,不需要比较构造函数或任何其他属性。
有趣的是,如果你有一个名为'location'的框架、图像或窗体,那么'document. properties '则为'location . properties '。location'提供了对框架窗口、图像或窗体的引用,而不是location对象。显然,这是因为文件。表单、文档。image和window.frames集合名称查找优先于window.location的映射。
<img name='location' src='location.png'>
if (document.location.tagName == 'IMG') alert('Hello!')
窗口。考虑到较老的浏览器,位置是两者中更可靠的一致性。
文档。Location ===窗口。Location返回true
also
Document.location.constructor === window.location.constructor为true
注:仅在Firefox 3.6、Opera 10和IE6上测试