-3

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!

cgee
  • 1,910
  • 2
  • 22
  • 38

2 Answers2

2

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    
}
Guillermo
  • 1,493
  • 3
  • 16
  • 35
0

try below code:

var locationVar = window.location.pathname;

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

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

hope this will help you.

Aefits
  • 3,399
  • 6
  • 28
  • 46
  • 1
    _window.location.pathname is an object._ no, it's string. Try following in console `> typeof window.location.pathname "string" > typeof location.pathname "string"` – Tushar Sep 30 '15 at 07:05
  • If I try it I got the message, that location is undefined. – cgee Sep 30 '15 at 07:07
  • Dont't forget to declare location variable. See my updated answer. – Aefits Sep 30 '15 at 07:10
  • @Shahid Now `var location = window.location.pathname;` and you're accessing `location.pathname` which will be `undefined` – Tushar Sep 30 '15 at 07:12
  • @Tushar your are right. Actuall problem is with the variable name location. I have updated the my answer. – Aefits Sep 30 '15 at 07:16
  • @Shahid same! Thanks. – cgee Sep 30 '15 at 07:20