Search code examples
javascriptif-statementpathname

what return 'windows.location.pathname'?


I tested something with the function window.locatin.pathname.

This is my js script:

var location = window.location.pathname;

console.log(location); // -> /de/immobilien-auf-mallorca/

if(location == "/de/immobilien-auf-mallorca"){
  console.log('true'); //doesn't work! It is not true???
}else{
  console.log('false'); //Output in my console    
}

I think that my var 'location' is a string and contains this string '/de/immobilien-auf-mallorca'.

But if I include an if statement (if location = /de/immobilien-auf-mallorca) I don't come into the first part of my if statement. (Take a look above)

I don't know why maybe my variable isn't a string?!

Maybe someone knows more about this.

Thanks for your help!


Solution

  • You have chosen a very particular reserved keyword to log ---> location, location defaults to window.location, which is an object. The solution is pretty simple, replace your variable name to something like "myLocation", that will make the trick.

    var myLocation = window.location.pathname;
    
    console.log(myLocation); // -> /de/immobilien-auf-mallorca
    
    if(myLocation == "/de/immobilien-auf-mallorca"){
      console.log('true'); //It's going to work....
    }else{
      console.log('false'); //Output in my console    
    }