2012-05-11 32 views
0

我想达到这样的事:jQuery的,如果URL匹配

if(window.location.href.indexOf("http://stackoverflow.com/questions/")){ 
... then to this 
} 

但是当URL看起来像这样http://stackoverflow.com/questions/ask/

+0

它仍然运行你问一个正则表达式来验证网址吗? – OlivierLi

+1

你的IF条件无论如何都是无效的......你应该这样做:if(window.location.href.indexOf(“http://stackoverflow.com/questions/”)> -1){ ...然后到这个 } –

+0

你会想调查indexOf() – kinakuta

回答

11
if(window.location.href === "http://stackoverflow.com/questions/"){ 
    ... then to this 
} 
+0

谢谢!看起来不错 – test

+1

我们不应该用'==='作比较吗? –

+0

@ RokoC.Buljan你是对的。谈到JavaScript时,依赖于==是一种糟糕的做法。要么存储值(=),要么检查严格等价(===)。接得好。 – blackhawk