views:

851

answers:

6

Should both of them reference to same object?

+1  A: 

Yes, they are the same. It's one of the many historical quirks in the browser JS API. Try doing:

window.location === document.location
Matthew Flaschen
+1  A: 

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

S.Mark
+3  A: 
rahul
Care to explain the reason for downvote?
rahul
historically, they are not (but didn't downvote)
Christoph
+1  A: 

window.location is the more reliably consistent of the two, considering older browsers.

Dave Ward
+6  A: 

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.

Christoph
A: 

how about document.port?

shammi