jQuery API

.ajaxStart()

.ajaxStart( handler() ) Returns: jQuery

Description: Register a handler to be called when the first Ajax request begins. This is an Ajax Event.

  • version added: 1.0.ajaxStart( handler() )

    handler()The function to be invoked.

Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() 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').ajaxStart(function() {
  $(this).text('Triggered ajaxStart 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 is sent, the log message is displayed.

Note: Because .ajaxStart() 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.

Example:

Show a loading message whenever an Ajax request starts (and none is already active).

$("#loading").ajaxStart(function(){
   $(this).show();
 });

Support and Contributions

Need help with .ajaxStart() 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 .ajaxStart()? Report it to the jQuery core team.

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

  • http://twitter.com/djrayon Reyo

    Before 1.4 you could attach a global ajaxStart like this:

    jQuery().ajaxStart(function(){
    alert(“it begins”);
    })

    Now you have to give the document to its selector

    jQuery(document).ajaxStart(function(){
    alert(“it begins”);
    })

    It took me a whole 5 minutes to figure this out :)

  • http://twitter.com/mmamedov Muhammed Mamedov

    Given the fact that ajaxStart is only called if there are no other ajax requests in progress, it makes is useless if you want to use it as an AJAX loader indicator.

    If you are loading external links via Ajax, and want to have a pretty loading icon in place, ajax loading indicator, while the content is being fethced in order to be inserted into DOM – ajaxStart() won’t be helpful. A better method to accomplish that would be to show() a loading indicator once say the $.load() is called, and in the callback function for that same $.load() to hide() the indicator.

    Is it an efficient way of doing it, or is there a better method?

    • Bob

      ajaxSend doesn't do it for you either?

    • Twoixter

      Muhammed, this is precisely the best event for attaching an AJAX loader indicator, but a global one like showing a spinner in a page corner or something. It helps you by avoiding tracking yourself the concurrent ajax calls.

      An example: you make 1 call and show the spinner, then you make another call before the first call returns and show the spinner again, then you must track both calls for completion. If not, you will hide the spinner before the second call returns.

      This is usually accomplished by using an active call count or by using this useful event.

  • http://twitter.com/rdLimosin rd Limosin

    before 1.4 i call a loading element:

    $().ajaxStart(function() {
    $('#loading').show();
    $('#result').hide();
    }).ajaxStop(function() {
    $('#loading').hide();
    $('#result').fadeIn('slow');
    });

    now must like this:
    $('#loading').ajaxStart(function() {
    $(this).show();
    $('#result').hide();
    }).ajaxStop(function() {
    $(this).hide();
    $('#result').fadeIn('slow');
    });

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

      For more information, see the section on Returning an Empty Set in the jQuery function entry.

      Instead of $().ajaxStart… you could do $(document).ajaxStart…

  • Ttttttt

    ffffffffff

  • Jonathan_alexey16

    Hello, who i cant get the bytes load and bytes total with jquery ajax… ??

  • Michael

    how can i determine other ajax request is running?
    and how can i stop it?

  • Richard Bladh

    If I want to have different indicators based on the DOM-target and / or caller. How would I go about and do that in a global context.

    It would be nice to have some kind of context-variable for ajaxStart / ajaxStop that indicates the “source” of the ajax-call.

  • Swright

    I want two different types of indicators, one a full screen indicator and the other an inline indicator. The inline indicator would be next to the element that triggered the event. I cannot figure out how to find the originating event or element so that I can determine which indicator to show. Any ideas on how to accomplish this short of calling the appropriate ajax indicator manually for every ajax request?

  • Liu_sl2005

    $(“#t”).ajaxStart(function(){
    alert(“ajax handler begin! “);
    });
    $(“#t1″).load(“test.txt”);
    $(“#t”).load(“test.txt”); //it's here

    ajaxStart:
    Register a handler to be called when the first Ajax request begins.
    ————————————
    ,but when the second Ajax request begin,ajaxStart will be invoked again?

  • fleeps

    How would I use ajaxstart and ajaxstop with $.post?
    Basically I want to show a loading gif…

    here's the code I'm using:
    $(“#mail-change input[type=submit]“).click(function(event){

    $.post('user_settings.php', $(“#mail-change”).serialize(), function(res) {

    $(res).insertBefore(“.grey”);

    }, 'html')

    });

  • Persevere4triumph

    This needs a better, more in-depth example, perhaps showing its use in context of the document, other contexts it may appear in and such. I'm no expert, so you can take this with a grain of salt, but I feel this is an important method that doesn't get much attention.

    Cheers

  • http://people.iola.dk/olau/ Ole Laursen

    Note that you can escape the event by setting global: false in $.ajax, e.g. if there's a background poll process you don't want to repeatedly show and hide a loading thing for.

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

    It is in the API, in the entry for jQuery. It’s unfortunate that you used that syntax along with this method, but it doesn’t make sense to warn people about it in this entry (it’s fine in the comments) without warning people in every other method of the API.

  • Asdas

    jh