Yeak
Yeak

Reputation: 2538

jquery check if page URL contains specific path

currently im using jquery to get the URL path

 var href = document.location.pathname;

the path im looking for is something like /clients/invoice/details/DynamicID/DynamicID

i need to check if jquery if the href contains /clients/invoice/details/ in that order in the path to do something. Can someone please help me - i tried the following

       if(href.match('/\/clients/invoice/details/\/')) {

       }

but i think im doing something wrong.

Upvotes: 0

Views: 4359

Answers (2)

zb22
zb22

Reputation: 3231

You can use the includes() method.

if(href.includes('/clients/invoice/details/')) {

}

ref: includes()

Upvotes: 0

Kshirodra Meher
Kshirodra Meher

Reputation: 228

var href = window.location.href;
if(href.indexOf(‘/clients/invoice/details/') > -1){
     //your code
}

You can do with a simple indexOf function instead of complex regex

Upvotes: 4

Related Questions