-1

I have the following ajaxError

$(document).ajaxError(function (e, xhr, settings) {
   if ( settings.url == "/users/sign_in" ) {
        if (xhr.status == 401) {
           $('#notice_signin').html(xhr.responseText);
           $('#notice_signin').addClass("alert").addClass("alert-error");
        }
      }
});

the thing is that settings.url has different params such as locale, source, etc.. so settings.url never matches /users/sign_in but is /users/sign_in?lang=fr&source=fb

what is an easy way to strip the params?

2
  • What is the purpose of stripping? Commented Nov 28, 2013 at 8:15
  • so that settings.url will be /users/sign_in and not /users/sign_in?lang=fr Commented Nov 28, 2013 at 8:16

4 Answers 4

1

if you need it only to check the url you can use indexOf method

if ( settings.url.toLowerCase().indexOf("/users/sign_in")>-1)
....

Otherwise if you want to have the url withouth parameters for later usage you can use split method

var url = settings.url.split('?')[0];
Sign up to request clarification or add additional context in comments.

Comments

1

Can you try using split

var urlString = '/users/sign_in?lang=fr&source=fb';
var urlArray =  urlString.split('?');
alert(urlArray[0]);
var settingsurl = urlArray[0];


if ( settings.url == "/users/sign_in" ) {
       ....
}

Comments

1
if (settings.url.split('?')[0] == '/users/sign_in') {
   ...
}

Note that this doesn't include error handling in case settings.url happens to be null.

As a side note, if '/users/sign_in' is the path of the current page, you might instead use window.location.pathname instead of hardcoding the value.

Comments

0

Or another option can use substring

if ( settings.url.substr(0,settings.url.indexOf("?")) == "/users/sign_in" ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.