JavaScript 中的 window.location 和 document.location 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2430936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between window.location and document.location in JavaScript?
提问by Morgan Cheng
Should both of them reference the same object?
他们都应该引用同一个对象吗?
采纳答案by rahul
According to the W3C, they are the same. In reality, for cross browser safety, you should use window.locationrather than document.location.
根据 W3C,它们是相同的。实际上,为了跨浏览器安全,您应该使用window.location而不是document.location.
回答by Christoph
The canonical way to get the current location object is window.location(see this MSDN page from 1996and the W3C draft from 2006).
获取当前位置对象的规范方法是window.location(参见1996 年的 MSDN 页面和2006年的 W3C 草案)。
Compare this to document.location, which originally only returned the current URL as a string (see this page on MSDN). Probably to avoid confusion, document.locationwas replaced with document.URL(see here on MSDN), which is also part of DOM Level 1.
将此与 比较document.location,后者最初仅以字符串形式返回当前 URL(请参阅MSDN 上的此页面)。可能是为了避免混淆,document.location被替换为document.URL(参见MSDN 上的此处),这也是DOM Level 1 的一部分。
As far as I know, all modern browsers map document.locationto window.location, but I still prefer window.locationas that's what I've used since I wrote my first DHTML.
据我所知,所有现代浏览器映射document.location到window.location,但我还是喜欢window.location因为这是自从我写我的第一DHTML我所用。
回答by Frédéric Hamidi
window.locationis read/write on all compliant browsers.
window.location在所有兼容的浏览器上都是读/写的。
document.locationis read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).
document.location在 Internet Explorer 中是只读的(至少),但在基于 Gecko 的浏览器(Firefox、SeaMonkey)中是读/写的。
回答by diEcho
document.locationwas originally a read-only property, although Gecko browsersallow you to assign to it as well. For cross-browser safety, use window.locationinstead.
document.location最初是一个只读属性,尽管Gecko 浏览器也允许您分配给它。为了跨浏览器的安全,请window.location改用。
Read more:
阅读更多:
回答by Phil Hamer
Interestingly, if you have a frame, image, or form named 'location', then 'document.location' provides a reference to the frame window, image, or form, respectively, instead of the Location object. Apparently, this is because the document.forms, document.images, and window.frames collection name lookup gets priority over the mapping to window.location.
有趣的是,如果您有一个名为“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!')
回答by AlphaMale
As far as I know, Both are same. For cross browser safety you can use window.locationrather than document.location.
据我所知,两者都是一样的。为了跨浏览器安全,您可以使用window.location而不是document.location.
All modern browsers map document.locationto window.location, but I still prefer window.locationas that's what I've used since I wrote my first web page. it is more consistent.
所有现代浏览器都映射document.location到window.location,但我仍然更喜欢,window.location因为这是我编写第一个网页后使用的。它更一致。
you can also see document.location === window.locationreturns true, which clarifies that both are same.
您还可以看到document.location === window.locationReturns true,这说明两者是相同的。
回答by YOU
document.location === window.locationreturns true
document.location === window.location返回 true
also
还
document.location.constructor === window.location.constructoris true
document.location.constructor === window.location.constructor是 true
Note: Just tested on , Firefox 3.6, Opera 10 and IE6
注意:刚刚在 、Firefox 3.6、Opera 10 和 IE6 上测试
回答by Matthew Flaschen
Yes, they are the same. It's one of the many historical quirks in the browser JS API. Try doing:
是的,它们是一样的。这是浏览器 JS API 中的众多历史怪癖之一。尝试做:
window.location === document.location
回答by Dave Ward
window.location is the more reliably consistent of the two, considering older browsers.
考虑到较旧的浏览器,window.location 是两者中更可靠的一致。
回答by Marquinho Peli
It's rare to see the difference nowadays because html 5 don't support framesets anymore. But back at the time we have frameset, document.location would redirect only the frame in which code is being executed, and window.location would redirect the entire page.
现在很少看到差异,因为 html 5 不再支持框架集。但是在我们有框架集的时候,document.location 只会重定向正在执行代码的框架,而 window.location 会重定向整个页面。

