Simple jQuery loader image function – attached to all ajax and form posts

So I needed a simple ajax loader box indicating when there was a ajax request going on in the page.

Fortunately jquery ajax have global events you can attach to enable this behavior.

Code:
Html at the end of masterpage perhaps (before </body>):

Ajax loader images can be found here: http://ajaxload.info/

Javascript:

The 3 first functions is attached to global ajax events. They are per default always raised by jquery ajax functions. More info: http://api.jquery.com/category/ajax/global-ajax-event-handlers/

The last function attaches to all form posts. (E.g. not ajax but might still be good to have).

Further extension would be to add more click event handlers. E.g. on certain a-elements perhaps. A problem though: if a user clicks a link the request will happen before the js. Therefore you must use e.preventDefault() and then show the loader. After that need to redirect the user to the desired url.

Add a script element for the js file in masterpage <head>:

Css:

Initially on every page load the loading box will be hidden. (Display: none).

Position fixed is needed so that the loaderbox always be placed in an fixed location relative to the browser window. Also enables the ability to let the box appear above the other html elements. (a high z-index).

Its then placed in the middle, vertically and horisontally: top: 50% and left: 50%.

A little opacity is added, a thin black border and rounded corners (radius). Padding adds white space between the image and the black border.

The negative margins is used to place the loader box in perfect center. Everything in css is placed according to topleft corner of the window as a startingpoint. The box is 82px wide (image is 32px wide and padding on both side is 50px (25+25px padding)).

This means the loadingbox and image will be slightly tilted to right and below the perfect center lines. We adjust this by setting negative margin to half the width and height of the box. (-41px respectively).

1 thought on “Simple jQuery loader image function – attached to all ajax and form posts”

  1. After some problems with a asp.net update panel control – the loading box/global ajax request were never closed or finished in a graceful manner. (loading box kept showing)

    I added simple css class deacctivation function on the ajax trigger element. E.g. add class “noAjaxLoaderBox” to the triggering element. Updated js code:

    //Generic loader image function.
    //Attached to global jquery ajax calls and form posts.

    $(document).bind(“ajaxStart”, function (event) {
    if (loadingBoxEnabled(event))
    $(“#loadingbox”).show();
    });
    $(document).submit(function (event) {
    if (loadingBoxEnabled(event))
    $(“#loadingbox”).show();
    });
    //$(document).bind(“ajaxSend”, function () {
    // $(“#loadingbox”).show();
    //});

    $(document).bind(“ajaxComplete”, function () {
    $(“#loadingbox”).hide();
    });
    $(document).bind(“ajaxError”, function () {
    $(“#loadingbox”).hide();
    });

    function loadingBoxEnabled(e) {
    var isLoadingBoxEnabled = true;
    if ($(e.currentTarget.activeElement).hasClass(“noAjaxLoaderBox”))
    isLoadingBoxEnabled = false;
    return isLoadingBoxEnabled;
    };

Leave a Reply