jQuery如何检查url是否包含单词? [英] jquery how to check if url contains word?
本文介绍了jQuery如何检查url是否包含单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够检查url是否包含单词catalog.
I want to be able to check if url contains the word catalogue.
这就是我正在尝试的...
This is what i am trying...
$(document).ready(function () {
if (window.location.href.indexOf("catalogue"))
{
$("#trail").toggle();
}
});
该网站的网址可能是..
The url of the site could be..
http://site.co.uk/catalogue/
或
http://site.co.uk/catalogue/2/domestic-rainwater.html
等
但是它不起作用.有人可以指出我要去哪里了吗?
But it doesn't work. Can somebody please point out where I'm going wrong?
推荐答案
尝试:
if (window.location.href.indexOf("catalogue") > -1) { // etc
indexOf不返回true/false,它返回搜索字符串在字符串中的位置;或-1(如果找不到).
indexOf doesn't return true/false, it returns the location of the search string in the string; or -1 if not found.
这篇关于jQuery如何检查url是否包含单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文