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?
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.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 is the more reliably consistent of the two, considering older browsers.
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.
document.location === window.location
returnstrue
also
document.location.constructor === window.location.constructor
istrue
Note: Just tested on , Firefox 3.6, Opera 10 and IE6
Yes, they are the same. It's one of the many historical quirks in the browser JS API. Try doing: