Should both of them reference to same object?
views:
851answers:
6Yes, they are the same. It's one of the many historical quirks in the browser JS API. Try doing:
window.location === document.location
document.location==window.location
returns true
also
document.location.constructor==window.location.constructor
is true
Note: Just tested on , Firefox 3.6, Opera 10 and IE6
window.location is the more reliably consistent of the two, considering older browsers.
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 with document.URL
(see here on MSDN), which is also part of DOM Level 1.
As far as I know, all modern browsers map document.location
to window.location
, but I still prefer window.location
as that's what I've used since I wrote my first DHTML.