test
test

Reputation: 2466

jQuery if url first segment contains

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

Answers (4)

Naveen
Naveen

Reputation: 230

var s1 = "foo";
alert(s1.indexOf("oo") != -1);

OR

if (/anyword/.test(self.location.href))
{
   ....
}

Upvotes: 0

Adil
Adil

Reputation: 148110

You can do it like this

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

if(myurl.split('/')[3] == "segment1")
{
//your code
}

Upvotes: 1

adeneo
adeneo

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

Gabe
Gabe

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

Related Questions