Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect 'any' ajax request being completed using jQuery?

I have a page where I can insert some javascript / jquery to manipulate the output. I don't have any other control over the page markup etc.

I need to add an extra element via jquery after each present on the page. The issue is that the elements are generated via an asynchronous call on the existing page which occurs after $(document).ready is complete.

Essentially, I need a way of calling my jquery after the page has loaded and the subsequent ajax calls have completed. Is there a way to detect the completion of any ajax call on the page and then call my own custom function to insert the additional elements after the newly created s ?

like image 491
Brian Scott Avatar asked May 26 '10 10:05

Brian Scott


People also ask

How do I know if AJAX request is completed?

If you are using xmlhttp object for ajax call then when xmlhttp. readyState==4 then it is consider as ajax request is completed.

How check AJAX request is successful jQuery?

$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });

How can I alert AJAX response data?

ajax({ url:"myscript. php", dataType: "json", success:function(data){ alert(data. cenas); } });


1 Answers

Unfortunately this doesn't apply since it seems the OP isn't using $.ajax() or any jQuery ajax method for actually loading content, but leaving it here in case future googler's are doing this.


You can use any of the global ajax events that meet your needs here, you're probably after $.ajaxComplete() or $.ajaxSuccess().

For example:

$(document).ajaxSuccess(function() {
  alert("An individual AJAX call has completed successfully");
});
//or...
$(document).ajaxComplete(function() {
  alert("ALL current AJAX calls have completed");
});

If you want to run just some generic function then attach them to document (they're just events underneath). If you want to show something in particular, for example a modal or message, you can use them a bit neater (though this doesn't seem to be what you're after), like this:

$("#myModal").ajaxComplete(function() {
  $(this).fadeIn().delay(1000).fadeOut();
});
like image 146
Nick Craver Avatar answered Sep 23 '22 16:09

Nick Craver