【问题标题】:Redirect user if url matches certain conditions如果 url 符合某些条件,则重定向用户
【发布时间】:2019-05-26 02:06:18
【问题描述】:

如果网址包含某些条件,我希望将用户重定向到新网页。我似乎无法让 OR 条件起作用。有什么想法吗?

if (
    document.location.href.indexOf("https://www.oct.ca/") > -1 &&
    document.location.href.indexOf("professionally_speaking") > -1 &&
    document.location.href.includes("2019-03" || "2018-12")
  ) {
    window.location = window.location.href.replace("https://www.oct.ca/", "http://professionallyspeaking.oct.ca/")

  }

【问题讨论】:

  • includes 方法不接受该布尔条件(始终为真)。读起来听起来不错,但它对 JavaScript 没有意义。检查文档。

标签: javascript string if-statement conditional-statements boolean-expression


【解决方案1】:

你不能那样做 - 添加另一个条件:

&& (document.location.href.includes("2019-03") || document.location.href.includes("2018-12"))

【讨论】:

  • 你需要括号括起来。事实上,如果 URL 是完全不需要的东西,例如 http://foo.bar/2018-12,这将通过。
  • 不用担心@user3175043,总是乐于提供帮助。如果我的回答解决了您的问题,请将其标记为已接受。
猜你喜欢
  • 2013-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 2012-08-05
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
相关资源
最近更新 更多