Is there any difference using only location using window.location in browsers

I always write:

console.log(window.location.href);

without even thinking about it. Most answers to SO also write like this. Is there a reason why I can't just write:

location.href

so how locationis the window level object? Are there any compatibility issues between browsers?

To clarify: I know what is document.location- NOT what it is about. This applies to whether there is a difference with using only locationvs using window.locationin browsers.

+4
source share
1 answer

There are some differences.

, :

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

, , , location. , , . , :

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

, , , , , , , , var, .

+9

All Articles