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


当前回答

现在很少能看到区别,因为html 5不再支持框架集了。但在那个时候我们有框架集,文档。Location将只重定向正在执行代码的帧和窗口。Location将重定向整个页面。

其他回答

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

阅读更多:

document.location

window.location

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

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

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

窗口。位置在所有兼容的浏览器上都是读/写的。

文档。location在Internet Explorer(至少)中是只读的,但在基于gecko的浏览器(Firefox, SeaMonkey)中是读/写的。

我更喜欢使用文档。位置,尽管位置,文档。位置和窗口。Location返回相同的对象。

使用文件的原因。地点是:

窗口的浏览器兼容性部分。位置提到

在Firefox 57之前,通过URL api访问URL时,URL中包含的单引号会被转义。参见bug 1386683。

文档的浏览器兼容性部分。位置提到

全力支持。

Mdn location reference uses document.location in their examples. // location: https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container const loc = document.location; console.log(loc.href); // https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container console.log(loc.protocol); // https: console.log(loc.host); // developer.mozilla.org:8080 console.log(loc.hostname); // developer.mozilla.org console.log(loc.port); // 8080 console.log(loc.pathname); // /en-US/search console.log(loc.search); // ?q=URL console.log(loc.hash); // #search-results-close-container console.log(loc.origin); // https://developer.mozilla.org:8080 location.assign('http://another.site') // load another page

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

also

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

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