8

I find myself always writing:

console.log(window.location.href);

without even thinking about it. The majority of answers on SO also write it this way. Is there any reason why I can't just write:

location.href

since location is an object at window level? Are there any cross-browser compatibility issues with this?

To Clarify: I know there is document.location - that is NOT what this question is about. This is about if there is any difference with using only location vs using window.location across browsers.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • 4
    Do you even read? It's not a duplicate of `location` vs `location.href`, but about `x` vs `window.x` – Tibos Oct 17 '13 at 10:00
  • 1
    @NikhilAgrawal Nope. You need to read the question again. Or the comment above yours. Both will explain why it is explicitly not about an object vs a property, but the scope of a particular object. – CodingIntrigue Jan 09 '14 at 15:13
  • 1
    People are getting way too close-happy. This question isn't even close to being related to the question it's supposedly a duplicate of. – Tmdean Mar 01 '14 at 23:48
  • @Tmdean: it's still a duplicate regardless, for example the first question in the "related" list. http://stackoverflow.com/questions/4709037/window-location-versus-just-location?rq=1 – Qantas 94 Heavy Mar 01 '14 at 23:53
  • @Qantas94Heavy The question you linked to is not the first linked one in the related list. None of those in the related list have anything to do with my question. Yours is completely valid but I searched for a long time and came up with nothing – CodingIntrigue Mar 02 '14 at 14:09
  • @RGraham: that's weird, that's what it shows up for me. Anyway, you're right -- the duplicates are incorrect. – Qantas 94 Heavy Mar 02 '14 at 14:11

1 Answers1

16

There are some differences.

In global scope, there is absolutely no difference between them, but in other cases you might get in trouble:

function () {
  var location = { 'href' : '123' } ;
  console.log(window.location.href) // actual url
  console.log(location.href) // '123'
}

This stems from the fact that if you write location without prefixing it with window, it will go up through every scope to find a variable named location. Eventually it will find it in window, unless another scope declared one as well. Obviously the reverse is true as well:

function () {
  var window = { 'location' : { 'href': '123' } };  
  console.log(window.location.href) // '123'
  console.log(location.href) // actual url
}

I for one prefer to prefix the global variables with window because that way i immediately know they are global and also because when i find a global variable that is not prefixed with window, i know it is a typo missing a var, but that is purely personal preference.

Tibos
  • 27,507
  • 4
  • 50
  • 64
  • 1
    Well, window is just a namespace, you just need to use it consistently and carefully. Often, people use `document.getElementById`, but somebody could also do `var document = 'Hello!'`, which does not have that function. However, window is sort of a keyword that you cannot override. So using `window.document.getElementById` you should be guaranteed to get the native javascript code. You could prefix this in your local scope with `var win=window,doc=win.document;`. Especially when using 3rd party code, and you care about safety, then this is the way to go imo (don't know if this is common). – Yeti May 13 '16 at 16:48