291

他们都应该引用同一个对象吗?

4

16 回答 16

264

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

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

于 2010-03-12T06:37:28.200 回答
221

获取当前位置对象的规范方法是window.location(参见1996 年的 MSDN 页面2006 年的 W3C 草案)。

将此与 比较document.location,它最初仅将当前 URL 作为字符串返回(请参阅MSDN 上的此页面)。可能是为了避免混淆,document.location被替换为document.URL(参见MSDN 上的此处),这也是DOM Level 1的一部分。

据我所知,所有现代浏览器都映射document.locationwindow.location,但我仍然更喜欢window.location它,因为这是我自编写第一个 DHTML 以来一直使用的。

于 2010-03-12T08:37:01.440 回答
95

window.location在所有兼容的浏览器上都是读/写的。

document.location在 Internet Explorer 中是只读的(至少),但在基于 Gecko 的浏览器(Firefox、SeaMonkey)中是读/写的。

于 2011-10-22T06:45:25.917 回答
48

document.location最初是一个只读属性,尽管Gecko 浏览器也允许您分配给它。为了跨浏览器的安全,请window.location改用。

阅读更多:

document.location

window.location

于 2011-10-22T06:46:38.193 回答
39

有趣的是,如果您有一个名为“location”的框架、图像或表​​单,那么“document.location”将分别提供对框架窗口、图像或表​​单的引用,而不是 Location 对象。显然,这是因为 document.forms、document.images 和 window.frames 集合名称查找优先于到 window.location 的映射。

<img name='location' src='location.png'>

if (document.location.tagName == 'IMG') alert('Hello!')
于 2012-08-23T19:44:30.483 回答
28

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

所有现代浏览器都映射document.locationwindow.location,但我仍然更喜欢window.location它,因为这是我写第一个网页以来一直使用的。它更加一致。

您还可以看到document.location === window.locationreturn true,这表明两者是相同的。

于 2011-10-22T06:47:49.223 回答
15

document.location === window.location返回true

document.location.constructor === window.location.constructortrue

注意:刚刚在 、Firefox 3.6、Opera 10 和 IE6 上测试过

于 2010-03-12T06:36:39.483 回答
12

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

window.location === document.location
于 2010-03-12T06:36:24.070 回答
8

考虑到较旧的浏览器,window.location 是两者中更可靠的一致。

于 2010-03-12T06:38:31.320 回答
4

现在很少看到差异,因为 html 5 不再支持框架集。但回到我们有框架集的时候,document.location 将只重定向正在执行代码的框架,而 window.location 将重定向整个页面。

于 2016-09-30T17:37:25.607 回答
3

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

document.URL 将返回“file://C:\projects\abc\a.html”

但 window.location.href 将返回“file:///C:/projects/abc/a.html”

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

于 2015-06-16T14:37:18.687 回答
3

嗯,是的,它们是一样的,但是......!

window.location不适用于某些 Internet Explorer 浏览器。

于 2016-08-04T20:35:35.730 回答
2

document.location.constructor === window.location.constructortrue

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

所以没有必要比较构造函数或任何其他属性。

于 2011-12-11T13:59:23.710 回答
2

我想说window.location的是获取当前URL的更可靠的方法。以下是在我在 URL 中附加哈希参数并稍后读取它的场景之一中,前面的window.location和之间的区别。document.url

在 URL 中添加哈希参数后。

在较旧的浏览器中,我无法通过使用从 URL 中获取哈希参数document.url,但是当我使用时,我window.location能够从 URL 中获取哈希参数。

所以总是最好使用window.location.

于 2012-08-30T06:49:43.113 回答
1

实际上,我注意到两者之间的 chrome 存在差异,例如,如果您想从子框架导航到沙盒框架,那么您可以只使用 document.location 而不是 window.location

于 2017-11-17T11:36:47.170 回答
1

尽管大多数人在这里推荐,但这就是Google Analytics的动态协议被剪断的样子(在他们最近从 ga.js 迁移到 analytics.js 之前):

ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

更多信息: https ://developers.google.com/analytics/devguides/collection/gajs/

在新版本中,他们使用'//',因此浏览器可以自动添加协议:

'//www.google-analytics.com/analytics.js'

因此,如果Google更喜欢document.locationwindow.location不是在 JS 中需要协议时,我想他们有一些原因。

总体:我个人认为document.location和是相同的,但如果使用document.location的Googlewindow.location等浏览器使用情况的统计数据最大的巨头,我建议关注他们。

于 2017-05-04T22:46:22.300 回答