what return 'windows.location.pathname'?

what return 'windows.location.pathname'?

本文关键字:pathname location windows what return      更新时间:2023-09-26

我用window. location .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语句的第一部分。(看上面)

我不知道为什么也许我的变量不是字符串?!

也许有人更了解这个。

谢谢你的帮助!

您选择了一个非常特殊的保留关键字来记录-> location, location默认为window。Location,它是一个对象。解决方案很简单,将变量名替换为类似"myLocation"的东西,这将使技巧。

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    
}

尝试下面的代码:

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    
    }