1

如果 url 包含这样的内容,并且我想如果 segment1 等于某物,则执行此操作...

http://stackoverflow.com/segment1/segment2

喜欢:

if (segment1 = 'anyword')
{
 ..then do this
}

类似的东西?

4

4 回答 4

7

您可以拆分网址:

var url = "http://stackoverflow.com/segment1/segment2";

var splits = url.replace('http://', '').split('/');

if(splits[1] == "segment1"){

}
于 2012-05-11T17:03:11.983 回答
1

你可以这样做

var myurl = "http://stackoverflow.com/segment1/segment2";

if(myurl.split('/')[3] == "segment1")
{
//your code
}
于 2012-05-11T17:07:05.433 回答
0
var s1 = "foo";
alert(s1.indexOf("oo") != -1);

或者

if (/anyword/.test(self.location.href))
{
   ....
}
于 2012-05-11T17:02:50.850 回答
0

您可以拆分 url,并检查每个路径名等,但如果您只是想检查 url 是否包含您可以执行的操作:

var url = 'http://stackoverflow.com/segment1/segment2';

if (url.indexOf('segment1') != -1) {
   //do this
}
于 2012-05-11T17:03:52.000 回答