-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?

Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244

4 Answers4

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];
giammin
  • 18,620
  • 8
  • 71
  • 89
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.

matk
  • 1,528
  • 2
  • 14
  • 25
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" ) {
       ....
}
Krish R
  • 22,583
  • 7
  • 50
  • 59
0

Or another option can use substring

if ( settings.url.substr(0,settings.url.indexOf("?")) == "/users/sign_in" ) 
commit
  • 4,777
  • 15
  • 43
  • 70