繁体   English   中英

当url是确切的url并且不包含其他参数时发出警报

[英]alert when url is exact url and not containing other parameters

我有两个网址:

  1. http://stackoverflow.com
  2. https://stackoverflow.com/questions/ask

我想在网址为http://stackoverflow.com而不是https://stackoverflow.com/questions/ask时发出警报。

if (location.href == "http://stackoverflow.com") {
   alert ('hello');
}

为什么当网址为https://stackoverflow.com/questions/ask时也会发出警报?

尝试这个

if (!window.location.pathname) {
   alert ('hello');
}

编辑

好的,答案是使用window.location.href而不是location.href,如下所示:

if (window.location.href == "http://stackoverflow.com") {
   alert ('hello');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM