Newbie
Newbie

Reputation: 153

Url string match in jQuery

Here i am trying this code,my url is like this "http://127.0.0.1/magento21/checkout/cart/add/uenc/aHR0cDovLzEyNy4wLjAuMS9tYWdlbnRvMjEvcHJvZHVjdDIuaHRtbA,,/product/2222/".

var url = settings.url;
if ("url:contains('checkout/cart/add')") {
    alert('sasas');
}

but this is not working

Upvotes: 0

Views: 189

Answers (2)

Shubham Khatri
Shubham Khatri

Reputation: 281774

USe this code to find whether url contains a given string:

var url = 'http://127.0.0.1/magento21/checkout/cart/add/uenc/aHR0cDovLzEyNy4wLjAuMS9tYWdlbnRvMjEvcHJvZHVjdDIuaHRtbA,,/product/2222/';

if (url.indexOf('checkout/cart/add') > -1)
 {
       alert("your url contains the name string");
    }

indexOf find the substring position if it exits in the given string. Running Demo here: jsfiddle

Upvotes: 1

hoffman
hoffman

Reputation: 42

U cannot just "url:contains('checkout/cart/add')". In order to check if url contains given pattern. U should write code like:

if (url.indexOf('checkout/cart/add') >= 0)

And it calls string's indexOf to check if it has given substring.

Upvotes: 1

Related Questions