skip to Main Content

I’m trying to run a function after a certain ajax request is completed.
Since there are multiple requests being made across the site, I’m filtering the one I want with settings.url like in the official documentation:

$( document ).ajaxComplete(function( event, xhr, settings ) {
  if ( settings.url === "ajax/test.html" ) {
    $( ".log" ).text( "Triggered ajaxComplete handler. The result is " +
      xhr.responseText );
  }
});

The actual url looks something like this https://example.com/page?ids=%2C1%2C2%2C
Everything after ?ids= is changing from request to request.

Now, the above code only fires if the settings.url is the exact same as in the request, id’s included.

if ( settings.url === "https://example.com/page?ids=%2C1%2C2%2C" )

So I need to match settings.url with https://example.com/page?ids= and a wildcard at the end, since the id’s are not important.

Would this be possible by using regex?

2

Answers


  1. Chosen as BEST ANSWER
    let regex = /[^ ]*pageids=[^ ]*/;
    
    if (settings.url.match(regex)) {
      console.log('triggered');
      console.log(settings.url);
    }
    

    It's working now. I forgot to replace settings.url=== with settings.url.match().


  2. If you want to do it with regex you could take Regex to parse an URL (What is a good regular expression to match a URL?) and finish it off with your delimiter, in your case the =.

    (www|http:|https:)+[^s]+[w]=
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search