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 button 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();
 });

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for .ajaxStart() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • 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?
  • Simone
    Note that AjaxStart will trigger ANY AjaxEvent. If you make something like this:

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

    It will not trigg ajax event for that element, but every ajax event.
  • 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 :)
  • Webdeveloper
    "It took me a whole 5 minutes to figure this out :)"

    Uaa, jesteś wyśmienicie zajebisty, człowieku.
  • Findus
    I had the same problem... The strange thing is that I didn't find anything on the official documentation about this change!

    Thanks!