Across the web, I see a vast number of JavaScript programmers writing window.location
instead of just location
. I was curious if anyone could offer an explanation as to why. window
is the global object, and therefore it is unnecessary to include -- isn't it? I mean, you don't see people write window.Math.floor
or new window.Date()
, so I'm curious as to why it would be specified with location
.
I understand that location
is considered to be a "property" of the window you're in, which I suppose makes some sense. But even so, I don't see any reason to specify the global object; it's not possible to overwrite location
in the first place, not without redirecting the page.
So, is this just a quirk that has been used for so long that it's become integrated with how we write JavaScript, or is there some tangible reason to do things this way? I checked Google, but alas, I came up with nothing...
They are actually identical. Technically, the "
window
" object IS the same thing as the root scope for Javascript variables.There's a big difference between
window.location
and the nativeMath
andDate
objects, which is thatMath
andDate
are native JavaScript objects that are specified to exist as properties of the global object, whilewindow.location
is a property of thewindow
host object (a host object is an object representing some aspect of the environment, provided by the environment, and is not subject to the same rules as native JavaScript objects. Other host objects includedocument
and any DOM element).window
in browsers serves two purposes: first, acting as the (well-specified) ECMAScript global object, and second, acting as a host object providing information about the browser environment. For uses ofwindow
in its host object capacity, I prefer to be explicit and provide thewindow.
prefix: the fact thatlocation
works without it is just a coincidence that comes fromwindow
's schizophrenic nature. Also, as pointed out by other answers, this also has the advantage of protecting you in the case when anotherlocation
variable exists in the current context.One good reason for not prefixing
Date
orMath
withwindow.
is that doing so creates code that does not work in a non-browser environment. Other environments generally do not providewindow
as an alias for the global object.It is not always just a matter of style - I was trying to load social media buttons asynchronously after the window's load event by appending script elements to a fragment, and then appending that fragment to the document. Twitter's widgets.js uses
location.href
in several places and was causing the following error in IE 8/9: Unexpected call to method or property access. I haven't figured out why, but this only happens when visiting the page via a link from another page. If you just append the script element to the head or usewindow.location.href
, this doesn't occur, so it appears to be some weirdness with IE 8/9 andcreateDocumentFragment()
.Example:
location is a property of the window object, so you can get it by requesting window.location. But if you don't specify an object, JavaScript assumes you want the window object. So just requesting location is the same as requesting window.location.
Partly for safety in case someone defines a
location
variable somewhere in the scope chain. thewindow.location
makes it an explicit reference to the property ofwindow
.Example: http://jsfiddle.net/dr6KH/
I always use
window.location
in my code for two principal reasons:window.
prefix reminds me that the variable is global and that others aren't.var location
somewhere in a containing scope (it's not an unlikely word to use as a variable name) and you'd be working on that instead.For me, clarity of purpose when coding is very important as it helps me avoid writing bugs and then helps me find them when I do.