2012-05-29 36 views
0

有无论如何检查window.location.href是否包含一些部分的字符或单词?JS检查url是否包含字或字符

想:如果没有找到

if(window.location.href.like("/users") or window.location.href.like("/profile")){ 
//do somenthing 
} 

回答

1

您可以使用.match并提供一个正则表达式:

if (window.location.href.match(/\/(users|profile)/)) { 
    alert('yes'); 
} 
1

window.location.href.search("something")是有用的第一场比赛的

if(window.location.href.search(/\/(users|profile)/) != -1) { // if not found 
    // do something 
} 

search()回报指数或-1,不返回布尔true/false

您还可以使用

if(!(/\/("users|profile")/.test(window.location.href))) { // if not found 
    // do something 
} 

.test()返回布尔值。

+0

嗯,似乎不工作 – sbaaaang

+0

@Ispuk它不会返回boolean值。看到我的更新 – thecodeparadox

+0

thx我更喜欢迈格尔脚本;)真的thx反正 – sbaaaang

相关问题