.ajaxComplete()S2C Home « Ajax « .ajaxComplete()

Ajax global completion handler.

Description

The .ajaxComplete() Ajax method, allows us to register an Ajax Event handler to be called when Ajax requests complete.

  • The .ajaxComplete() method callbacks are executed via the ajaxComplete event which is triggered every time an Ajax request completes. For finer grained control of callback execution when the .ajaxComplete() Ajax method is fired, interrogate the event and jqXHR objects or the ajaxSettings parameters passed to the method.

Syntax

Signature Description
.ajaxComplete( handler(event, jqXHR, ajaxSettings) )Register an Ajax Event handler to be called when Ajax requests complete.

Parameters

Parameter Description Type
handler(event, jqXHR, ajaxSettings) The function to be called.
  • Each time the callback runs, it is passed the event and jqXHR objects as well as the ajaxSettings pertaining to the request.
  • The callback is fired in the context of the current jQuery instance, so the this special operator refers to that instance.
Function

Return

A jQuery object.

.ajaxComplete(handler(event, jqXHR, ajaxSettings)) ExampleTop

Register an Ajax Event handler to be called when Ajax requests complete.

In the example below when we press the button the first time we use the .ajaxComplete() method to set up a global event handler to be run for each Ajax request. The handler is fired at the end of each request and we output a message giving some information about the request.


$(function(){
  $('#btn1').one('click', function(){
    // Register global event handler and attach to 'div1' element
    $('#div1').ajaxComplete(function(event, jqXHR, ajaxSettings) {
       $(this).append('Triggered the ajaxComplete() global event handler for url: ' 
                      + ajaxSettings.url + '. Status: ' + jqXHR.status + '. Status text: ' 
                      + jqXHR.statusText + '.<br><br>');
    });
    $.ajax( "../js/get1.js", {
      dataType: "script",
      success: function() { 
        $('#div1').append('The request was a success! <br>');
      }
    });
    $.getScript( "../js/get6.js" );
    $.post( "../js/post1.js" );
  });
});

div1. Some initial text.

Press the button below to action the above code: