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


当前回答

是的,他们是一样的,但是....!

窗口。location在某些Internet Explorer浏览器上无法工作。

其他回答

根据W3C的说法,它们是相同的。实际上,为了跨浏览器的安全性,您应该使用window。Location而不是document.location。

见:http://www.w3.org/TR/html/browsers.html dom-location

实际上,我注意到两者之间的chrome的区别,例如,如果你想做一个导航到沙盒框架从一个子框架,那么你可以这样做,只是与文档。Location,但不是window。Location

至少在IE中,它在本地文件上有一点不同:

文档。URL将返回 ”文件:/ / C: \ \ abc \ a.html项目”

但是window。location。href会返回 “文件:/ / / C: / / abc / a.html”项目

一个是反斜杠,一个是正斜杠。

我更喜欢使用文档。位置,尽管位置,文档。位置和窗口。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

据我所知,两者是一样的。为了跨浏览器安全,您可以使用window。Location而不是document.location。

所有现代浏览器都映射文档。窗口的位置。不过我还是喜欢靠窗的。自从我写了我的第一个网页,我就一直使用这个地址。它更加一致。

你也可以看到文档。Location ===窗口。Location返回true,这说明两者是相同的。