[英]alert when url is exact url and not containing other parameters
我有两个网址:
我想在网址为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.