jQuery Example: Ajax activity indicator

Rick Strahl has been doing some great posts about jQuery on his blog. Yesterday he posted about an excellent resource, ajaxload.info, for creating ajax activity indicators. The second jQuery example that I’d like to share concerns how to display those images on each ajax call. Here is the relevant jQuery code:

The first two jQuery snippets are placed in the $(document).ready function. The HTML body has a div appended to it that will hold the loading image. We then set the CSS properties of that div to hide the image and define the placement.

The last snippet is outside of the $(document).ready function and serves to bind the visibility of the ajaxStart and ajaxStop jQuery events.

Now, everytime an ajax call is placed back to the server, a animated gif image will display in the upper right hand corner of the page to indicate background ajax activity. When the request is finished, the image will hide itself again.

See the demo and view the source to learn more. In this demo, your message is POSTed via ajax to the server which sleeps for 3 seconds then outputs your message into a message box. Make sure to peak at the upper right side of the page to see the activity indicator.

Thanks go out to jQuery for their awesome framework and Mike Alsup, the jQuery Form plugin author, for such a great plugin

11 thoughts on “jQuery Example: Ajax activity indicator

  1. Hi Shaun ,

    Nice One !! This is very nice example of Ajax Load Indicator .I was really looking for such stuff since long time . I tried this example but still not getting any output this is my code

    First jQuery Test

    $(“body”).append(‘

    ‘);

    $(‘#ajaxBusy’).css({
    display:”none”,
    margin:”0px”,
    paddingLeft:”0px”,
    paddingRight:”0px”,
    paddingTop:”0px”,
    paddingBottom:”0px”,
    position:”absolute”,
    right:”3px”,
    top:”3px”,
    width:”auto”
    });

    $(document).ajaxStart(function () {
    $(‘#ajaxBusy’).show();
    }).ajaxStop(function (){
    $(‘#ajaxBusy’).hide();
    });

    This is My Changed Test Page

    PLease help me i really need it !!

  2. I have tried everything getting this to work, then again, I am new to JQuery. Both IE and FF not working with it. I placed the first two snippets in the $(document).ready function and the ajaxStart and ajaxStop out of the .ready function. Also, just curious, if there is no call to the server, but a function is working, does it still show the indicator?

  3. The last part works much better this way:

    $(document).ajaxStart(function () {
    $(’#ajaxBusy’).show();
    });
    $(document).ajaxStop(function (){
    $(’#ajaxBusy’).hide();
    });

  4. @sean: Why do you think that? Works better how? Why not take advantage of jQuery chaining, it’s faster.

  5. @frank: An ajax call must be made to the server for it to work. If you are testing locally, i.e. http://localhost, the request could be quick enough that the indicator doesn’t have time to show up.

  6. How do i show a loading screen for different divs without witting different methods for each
    eg suppose if i have 4 divs
    news
    articles
    downloads
    gallery
    the data for this sections will come from a webservice using ajax method how do i show a loading sreen for each of this section

  7. Pingback: Javascript spinning wait hourglass-type thing - MicroEducate

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.