0

Why is it that some pages in my web project works simply by using location.href in the onclick function while some require me to enter window.location.href for redirection to work. I've read out the scope related aspects related to using window. prefix to location.href but I'm still unable to comprehend this fully.

Any information on this will be highly appreciable.

Cheers!

falinsky
  • 7,229
  • 3
  • 32
  • 56
Sunny
  • 1
  • 3
  • 1
    it will varies based on the context. Refer:https://stackoverflow.com/questions/19423513/is-there-any-difference-with-using-only-location-vs-using-window-location-across – Aravind S Jun 26 '18 at 08:35
  • similar question https://stackoverflow.com/questions/9903659/difference-between-window-location-and-location-href – manny Jun 26 '18 at 08:35

1 Answers1

3

The only thing that would make location.href not work when window.location.href does work is if you've shadowed the location identifier by declaring one in a nearer scope than the global one, like this:

location.href = "..."; // would work here...
function foo() {
    var location = 42;
    location.href = "..."; // ...but not here

    function bar() {
        location.href = "..."; // ...nor here
    }

    bar();
}
foo();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875