jQuery API

.ajaxComplete()

.ajaxComplete( handler(event, XMLHttpRequest, ajaxOptions) ) Returns: jQuery

Description: Register a handler to be called when Ajax requests complete. This is an Ajax Event.

  • version added: 1.0.ajaxComplete( handler(event, XMLHttpRequest, ajaxOptions) )

    handler(event, XMLHttpRequest, ajaxOptions)The function to be invoked.

Whenever an Ajax request completes, jQuery triggers the ajaxComplete event. Any and all handlers that have been registered with the .ajaxComplete() method are executed at this time.

To observe this method in action, we can set up a basic Ajax load request:

<div class="trigger">Trigger</div>
<div class="result"></div>
<div class="log"></div>

We can attach our event handler to any element:

$('.log').ajaxComplete(function() {
  $(this).text('Triggered ajaxComplete handler.');
});

Now, we can make an Ajax request using any jQuery method:

$('.trigger').click(function() {
  $('.result').load('ajax/test.html');
});

When the user clicks the element with class trigger and the Ajax request completes, the log message is displayed.

Note: Because .ajaxComplete() is implemented as a method of jQuery object instances, we can use the this keyword as we do here to refer to the selected elements within the callback function.

All ajaxComplete handlers are invoked, regardless of what Ajax request was completed. If we must differentiate between the requests, we can use the parameters passed to the handler. Each time an ajaxComplete handler is executed, it is passed the event object, the XMLHttpRequest object, and the settings object that was used in the creation of the request. For example, we can restrict our callback to only handling events dealing with a particular URL:

Note: You can get the returned ajax contents by looking at xhr.responseXML or xhr.responseHTML for xml and html respectively.

$('.log').ajaxComplete(function(e, xhr, settings) {
  if (settings.url == 'ajax/test.html') {
    $(this).text('Triggered ajaxComplete handler. The result is ' +
                     xhr.responseHTML);
  }
});

Example:

Show a message when an Ajax request completes.

$("#msg").ajaxComplete(function(event,request, settings){
   $(this).append("<li>Request Complete.</li>");
 });

Support and Contributions

Need help with .ajaxComplete() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .ajaxComplete()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • Guest

    I think it's correct, but I don't know, is the ajaxComplete global? in this example, I read it as this:

    for the div with class result, load ajax/test.html, when the div with class log is done loading, trigger an event.

    Is it correct?

    • Guest

      Yes..correct..

      • Guest

        first class with 'trigger' performs click event and in respond to that 'click' event class with 'result' loads html content and then finally class with 'log' displays message because ajax response is complete.

        • http://pulse.yahoo.com/_YQSVRFKE6JDFUBOSECKO4S6CLA sachin

          first comments and not a good for this

  • Lzjlovechina

    I'm chinese,this is a good page! haha………..

  • Lzjlovechina

    I'm chinese,this is a good page! haha………..

  • Lzjlovechina

    write less do more ….good!

  • Chinese

    have a chinese version

  • keven

    does any one know what is the parameter “event” supposed to be, and how to use it?

    • http://www.learningjquery.com/ Karl Swedberg

      It's the event object. The ajaxComplete method is treated as a special event, so you can have access to the event object just as you would with any other event methods.

  • Jason

    For things like showing/hiding a load indicator, I typically use a function in the ':beforeSend' and ':complete' settings for $.ajax. If that is not worth mentioning, feel free to remove my comment.

  • wacalu

    http://jsfiddle.net/wacalu/Jscba/
    why does this get called on every element?
    I only want to hook into elements which call xhr requests
    :(

  • asdlkfh

    kjhkh

  • Fjfg

    df

  • Chris

    The non-global ajax complete handler takes “textStatus” as one of it's arguments. One can check this for timeout, etc. How does one access the status using the global handler?

  • abdullah raza lakhan

    hi i want to jquery where should i start

    abdullah raza lakhan

  • http://pulse.yahoo.com/_YQSVRFKE6JDFUBOSECKO4S6CLA sachin

    first comments and not a good for this

  • http://pulse.yahoo.com/_YQSVRFKE6JDFUBOSECKO4S6CLA sachin

    hjkhjkhjk

  • Adityajain

    good job

  • Muhammednuzly

    do we need to install any software to write and test programs in AJAX?

    • Christian

      Shut up stupid terrorist. You wanna commit another 9/11 in jQuery? Go to hell…

      • Dd

        Christian you suck

      • ArnoldPalmer

        @Christian (Ironic how undeserving you are of this name). This is a jQuery comment board about returning AJAX. The OP asked a question, however you were too busy:

        1. Making wild generalizations about a culture you [obviously] know very little about.
        2. Judging people based on what you've been spoon-fed.
        3. Using violence as a way to express yourself, and getting nowhere.
        4. Perpetuating stereotypes.
        5. Being rude.

        Christian, by my math, you've got what it takes to be a terrorist. Educate yourself and/or grow up. Thanks!

        @Muhammednuzly : No, additional software is not necessary. JQuery is fully-capable of handling AJAX: More here: http://api.jquery.com/jQuery.a…/