【发布时间】:2015-12-27 20:47:30
【问题描述】:
我用函数 window.locatin.pathname 测试了一些东西。
这是我的 js 脚本:
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
}
我认为我的 var 'location' 是一个字符串并且包含这个字符串 '/de/immobilien-auf-mallorca'。
但如果我包含一个 if 语句 (if location = /de/immobilien-auf-mallorca),我不会进入我的 if 语句的第一部分。 (看上面)
我不知道为什么我的变量可能不是字符串?!
也许有人对此了解更多。
感谢您的帮助!
【问题讨论】:
-
试试
if(decodeURIComponent(location) == "/de/immobilien-auf-mallorca"){ -
试过你的代码。对我来说很好。
-
@cgee 检查
length的路径名和要比较的字符串长度 -
@Tushar window.location = 28 和 if 语句中的字符串 = 27.. 但是为什么???
-
@cgee 不能从这里说可能隐藏的字符,请在位置尝试
trim
标签: javascript if-statement pathname