window.location 和 document.location 有什么区别?
window.location 和 document.location 有什么区别?它们都应该引用同一个对象吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
window.location 和 document.location 有什么区别?它们都应该引用同一个对象吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(18)
根据 W3C 的说法,它们是相同的。实际上,为了跨浏览器安全,您应该使用
window.location
而不是document.location
。请参阅:http://www.w3.org/TR/html/browsers .html#dom-位置
According to the W3C, they are the same. In reality, for cross browser safety, you should use
window.location
rather thandocument.location
.See: http://www.w3.org/TR/html/browsers.html#dom-location
获取当前位置对象的规范方法是
window.location
(请参阅 1996 年的此 MSDN 页面 和 W3C 草案从 2006 年开始)。将此与
document.location
进行比较,后者最初仅以字符串形式返回当前 URL(请参阅 MSDN 上的此页面)。可能是为了避免混淆,document.location
被替换为document.URL
(请参阅 MSDN 上),它也是 DOM 级别 1。据我所知,所有现代浏览器都会将
document.location
映射到window.location
,但我仍然更喜欢window.location
,因为这就是我想要的自从我编写第一个 DHTML 以来就一直在使用。The canonical way to get the current location object is
window.location
(see this MSDN page from 1996 and the W3C draft from 2006).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.location
was replaced withdocument.URL
(see here on MSDN), which is also part of DOM Level 1.As far as I know, all modern browsers map
document.location
towindow.location
, but I still preferwindow.location
as that's what I've used since I wrote my first DHTML.window.location 在所有兼容的浏览器上均可读取/写入。
document.location 在 Internet Explorer 中是只读的(至少),但在 Gecko 中是读/写的-基于浏览器(Firefox、SeaMonkey)。
window.location is read/write on all compliant browsers.
document.location is read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).
document.location
最初是只读属性,尽管 Gecko 浏览器 也允许您对其进行分配。为了跨浏览器安全,请改用window.location
。了解更多:
document.location
window.location
document.location
was originally a read-only property, although Gecko browsers allow you to assign to it as well. For cross-browser safety, usewindow.location
instead.Read more:
document.location
window.location
有趣的是,如果您有一个名为“location”的框架、图像或表单,则“document.location”分别提供对框架窗口、图像或表单的引用,而不是 Location 对象。显然,这是因为 document.forms、document.images 和 window.frames 集合名称查找优先于映射到 window.location。
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.
据我所知,两者是相同的。为了跨浏览器安全,您可以使用
window.location
而不是document.location
。所有现代浏览器都将
document.location
映射到window.location
,但我仍然更喜欢window.location
,因为这是我写这篇文章以来一直使用的我的第一个网页。它更加一致。您还可以看到
document.location === window.location
返回true
,这表明两者是相同的。As far as I know, Both are same. For cross browser safety you can use
window.location
rather thandocument.location
.All modern browsers map
document.location
towindow.location
, but I still preferwindow.location
as that's what I've used since I wrote my first web page. it is more consistent.you can also see
document.location === window.location
returnstrue
, which clarifies that both are same.document.location === window.location
返回true
并且
document.location.constructor === window.location.constructor
是true
注意:刚刚在 、Firefox 3.6、Opera 10 和 IE6 上测试
document.location === window.location
returnstrue
also
document.location.constructor === window.location.constructor
istrue
Note: Just tested on , Firefox 3.6, Opera 10 and IE6
是的,它们是一样的。这是浏览器 JS API 中众多历史怪癖之一。尝试做:
Yes, they are the same. It's one of the many historical quirks in the browser JS API. Try doing:
考虑到较旧的浏览器,window.location 是两者中更可靠一致的。
window.location is the more reliably consistent of the two, considering older browsers.
现在很少能看到差异,因为 html 5 不再支持框架集。但在我们有框架集的时候,document.location只会重定向正在执行代码的框架,而window.location会重定向整个页面。
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.
document.location.constructor === window.location.constructor
为true
。这是因为它与您在
document.location===window.location
中看到的对象完全相同。因此无需比较构造函数或任何其他属性。
document.location.constructor === window.location.constructor
istrue
.It's because it's exactly the same object as you can see from
document.location===window.location
.So there's no need to compare the constructor or any other property.
至少在 IE 中,它对本地文件有一点不同:
document.URL 将返回
"file://C:\projects\abc\a.html"
但 window.location.href 将返回
"file:///C:/projects/abc/a.html"
一种是反斜杠,一种是正斜杠。
At least in IE, it has a little difference on local file:
document.URL will return
"file://C:\projects\abc\a.html"
but window.location.href will return
"file:///C:/projects/abc/a.html"
One is back slash, one is forward slash.
是的,它们是相同的,但是......!
window.location
无法在某些 Internet Explorer 浏览器上运行。Well yea, they are the same, but....!
window.location
is not working on some Internet Explorer browsers.tl;dr 它们实际上总是相同的。首选
window.location
。它们什么时候不同?如果文档未完全激活。例如:
tl;dr They are virtually always the same. Prefer
window.location
.When are they different? If the document is not fully active. For example:
我想说
window.location
是获取当前 URL 的更可靠方法一个>。以下是我在 URL 中附加哈希参数并稍后读取它的场景之一中前面的
window.location
和document.url
之间的区别。在 URL 中添加哈希参数后。
在较旧的浏览器中,我无法使用
document.url
从 URL 获取哈希参数,但是当我使用window.location
时,我能够获取来自 URL 的哈希参数。因此,最好使用
window.location
。I would say
window.location
is the more reliable way of getting the current URL.Following is the difference between the
window.location
anddocument.url
that came in front in one of the scenarios where I was appending hash parameters in the URL and reading it later.After adding hash parameters in the URL.
In an older browser, I was not able to get the hash parameters from the URL by using
document.url
, but when I usedwindow.location
then I was able to get the hash parameters from the URL.So it's always better to use
window.location
.我更喜欢使用
document.location
,即使location
、document.location
和window.location
返回相同的对象。使用
document.location
的原因是:Mdn 位置参考使用
document.location
在他们的示例中。I prefer using
document.location
, even thoughlocation
,document.location
, andwindow.location
returns same object.Reasons for using
document.location
are:Mdn location reference uses
document.location
in their examples.尽管大多数人都在这里推荐,但这就是 Google Analytics 的动态协议剪裁的样子(在他们最近从 ga.js 迁移到 Analytics.js 之前):
更多信息:< /em> https://developers.google.com/analytics/devguides/collection/gajs/
在新版本中,他们使用“//”,以便浏览器可以自动添加协议:
所以如果 Google 当他们需要 JS 中的协议时,他们更喜欢 document.location 而不是
window.location
,我猜他们有一些原因。总体:我个人认为
document.location
和window.location
是相同的,但如果拥有关于浏览器使用情况的最大统计数据,例如 < strong>Google 使用 document.location,我建议关注它们。Despite of most people recommend here, that is how Google Analytics's dynamic protocol snipped looked like for ages (before they moved from ga.js to analytics.js recently):
More info: https://developers.google.com/analytics/devguides/collection/gajs/
In new version they used '//' so browser can automatically add protocol:
So if Google prefers document.location to
window.location
when they need protocol in JS, I guess they have some reasons for that.OVERALL: I personally believe that
document.location
andwindow.location
are the same, but if giant with biggest stats about usage of browsers like Google using document.location, I recommend to follow them.实际上,我注意到两者之间的 chrome 存在差异,例如,如果您想从子框架导航到沙盒框架,那么您可以仅使用 document.location 而不是使用 window.location 来执行此操作
Actually I notice a difference in chrome between both , For example if you want to do a navigation to a sandboxed frame from a child frame then you can do this just with document.location but not with window.location