【发布时间】:2016-10-05 10:23:48
【问题描述】:
我想查看我当前的网址,例如:
http://www.dummy.com/subpageone/pagetwo.html?ineedthis[andthis]&butnotthis
如果我得到的是 ineedthis[andthis],我想要一个 alert
到现在为止
if (window.location.href.indexOf("ineedthis") > -1) {
alert("your url contains it");
}
但这还不够。如何查看[andthis]?我已经尝试了一些变化,但完全没有运气。
indexOf("ineedthis\[andthis\]")
indexOf("ineedthis%5andthis%5D")
if (/ineedthis\[andthis\]/.test(window.location.href))
感谢您的任何建议。
【问题讨论】:
-
indexOf("ineedthis[andthis]")对我来说很好 - 在您提供的示例 URL 中返回索引 45。此外,无需转义方括号。 -
可能是您使用
window.location.href编码的网址。在比较之前先尝试解码。
标签: javascript url contains square-bracket