Should both of them reference the same object?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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
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
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.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.
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.
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