我想看看我現在的網址,例如:檢查URL包含文本和括號
http://www.dummy.com/subpageone/pagetwo.html?ineedthis[andthis]&butnotthis
我想要一個alert
如果我得到確切ineedthis[andthis]
到目前爲止我
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]」)'對我來說工作正常 - 回報指數45,在你給的例子URL。另外,不需要轉義方括號。 – Utkanos
可能是你用'window.location.href'獲得編碼url。在比較之前先嚐試解碼它。 – vijayP