【问题标题】:Changing == to === in if(window.location == 'x') causes the block not to execute在 if(window.location == 'x') 中将 == 更改为 === 会导致块不执行
【发布时间】:2013-09-26 03:59:45
【问题描述】:

我正在检查我的代码是否使用了== 而不是===,但更改了这一行:

if(window.location == 'app:/test.html')

到这里:

if(window.location === 'app:/test.html')

块中的结果不再被执行。

正确的做法是什么?

【问题讨论】:

    标签: javascript equals-operator triple-equals


    【解决方案1】:

    这是因为===匹配类型和内容。

    window.location 在大多数情况下类似于string,但实际上是Location 对象。

    您可以更改if 以检查href 属性,即string

    if(window.location.href === 'app:/test.html')
    

    然后您的代码将按预期工作!

    MDN 有 a decent article 关于 window.location 值得一读。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-12
      • 2019-06-17
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多