.ajaxStart()

jQuery

.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()
      Type: Function()
      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, set up a basic Ajax load request:

1
2
3
                                
<div class="trigger">Trigger</div>
<div class="result"> </div>
<div class="log"> </div>

Attach the event handler to any element:

1
2
3
                                
$(document).ajaxStart(function() {
$( ".log" ).text( "Triggered ajaxStart handler." );
});

Now, make an Ajax request using any jQuery method:

1
2
3
                                
$( ".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, you can use the this keyword to refer to the selected elements within the callback function. As of jQuery 1.8, however, the .ajaxStart() method should only be attached to document.

Additional Notes:

  • If $.ajax() or $.ajaxSetup() is called with the global option set to false, the .ajaxStart() method will not fire.

Example:

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

1
2
3
                                  
$(document).ajaxStart(function() {
$( "#loading" ).show();
});