Reputation: 2466
if url contains like this and I want to if segment1 equals something then do this...
http://stackoverflow.com/segment1/segment2
like:
if (segment1 = 'anyword')
{
..then do this
}
something like that?
Upvotes: 1
Views: 2815
Reputation: 230
var s1 = "foo";
alert(s1.indexOf("oo") != -1);
OR
if (/anyword/.test(self.location.href))
{
....
}
Upvotes: 0
Reputation: 148110
You can do it like this
var myurl = "http://stackoverflow.com/segment1/segment2";
if(myurl.split('/')[3] == "segment1")
{
//your code
}
Upvotes: 1
Reputation: 318182
You could split the url, and check each pathname etc. but if you're only trying to check if the url contains something you could just do:
var url = 'http://stackoverflow.com/segment1/segment2';
if (url.indexOf('segment1') != -1) {
//do this
}
Upvotes: 0
Reputation: 50493
You could split up the url:
var url = "http://stackoverflow.com/segment1/segment2";
var splits = url.replace('http://', '').split('/');
if(splits[1] == "segment1"){
}
Upvotes: 7