Rick Helston
Rick Helston

Reputation: 763

If in Partial URL

I know I can do this to match a full URL:

if(window.location.href == 'http://domain.com/folder/'){
    // Do something.
}

But how can I check for a partial URL like this?

if(window.location == '/folder/'){
    // Do something.
}

Upvotes: 1

Views: 706

Answers (4)

jonhurlock
jonhurlock

Reputation: 1908

You can just check to see if it is a substring of the window location (which we can convert to a string):

if((window.location.toString()).indexOf("/folder/") > -1){
    // do something
}

Upvotes: 0

jmargolisvt
jmargolisvt

Reputation: 6088

I think you are looking for location.pathname. This will return the url you are looking for.

Upvotes: 1

kailash sharma
kailash sharma

Reputation: 356

You can directly use

location.pathname this will return you path after hostname

Kindly refer this link

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_loc_pathname

Upvotes: 4

jfriend00
jfriend00

Reputation: 707198

You can just use normal string comparison or searching functions.

if (window.location.href.indexOf('/folder/' !== -1) {
    // found partial match
}

Or, you can be more specific if you want it to be only at the top part of the path:

if (window.location.href.indexOf('http://domain.com/folder/' !== -1) {
    // found partial match
}

Or, if you want to see if the path starts with "/folder/", you can use a regex on just the pathname:

if (window.location.pathname.test(/^\/folder\//)) {
    // pathname starts with /folder/
}

Upvotes: 1

Related Questions