Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call jquery ajaxStart + ajaxComplete

Tags:

jquery

ajax

I have a load function and would like for the code to write some html into a div while it loads, and when it completes, to display the page. I saw some little write-ups on the ajaxStart and ajaxComplete events, however I am not sure how to implement them.

Here is the jQuery I am thinking of using, however not sure how to implement within the code I have currently…

$(document).ajaxStart(function(){ 
    $('#content').html("Loading Content...");
});

Here is the current jQuery I am using:

//Load content
$(".load").click(function(){
    $("#content").empty();           
    loadName = $(this).attr("id");
    $("#content").load("/content/" + loadName + ".php");
});
like image 343
antonanton Avatar asked Sep 16 '09 01:09

antonanton


People also ask

What is ajaxComplete in jQuery?

The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.

What is ajaxStart in jQuery?

Definition and Usage The ajaxStart() method specifies a function to be run when an AJAX request starts. Note: As of jQuery version 1.8, this method should only be attached to document.

Can jQuery send asynchronous HTTP requests?

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.

How check AJAX call is completed in jQuery?

jQuery ajaxStop() Method The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.


1 Answers

If it's a single div and you would like to update its contents with a loading message/indicator, you can do so like this:

$("#content").html('<strong>Loading...</strong>')
             .load("/content/" + loadName + ".php");

I wouldn't use the ajaxStart and ajaxStop/ajaxComplete global events unless you have a common loading indicator for all ajax calls. That said, it can be done as follows:

$("#loading").bind("ajaxStart", function(){
    $(this).show();
}).bind("ajaxStop", function(){
    $(this).hide();
});

where the loading element is whatever you want to reveal during an ajax call.

like image 116
karim79 Avatar answered Nov 01 '22 07:11

karim79