Well, it seems in jQuery this is pretty easy. Check the code below, as it is all it took.
busyDialog = { init: function () { var el = $(document.createElement('div')).attr('id', 'busyDialog') .css({ 'width': '100%', 'height': '100%', 'background-image': 'url(lib/images/ajax-loader.gif)', 'background-position': 'center', 'background-repeat': 'no-repeat', 'position': 'fixed', 'z-index': '9999', 'display': 'none', 'cursor': 'progress' }); $(el).ajaxStart(function(){ $(this).show(); }); $(el).ajaxStop(function(){ $(this).hide(); }); $('body').prepend(el); } }
Personally, I have this thing loaded into a widget class. When the app instantiates I call the init method and I'm set. This would be sickeningly easy to prototype into jQuery as a plugin.
What happens in the code above is that we create a new div and place a loading animated gif in the center of it. You can get an animated gif over at AjaxLoad. The div is created at 100% width and height so that the use cannot interact with the screen while the loading dialog is active. The loading div is initially invisible. Two event handlers (ajaxStart and ajaxStop) are employed, which is where the real helpfulness of jQuery comes in. jQuery allows us to specify global handlers (here showing and hiding the loading div) for all Ajax requests. We are still able to have our custom success handlers for each individual Ajax call. Wicked.
The final step is to throw the div into the DOM. We do this as the first child of 'body'.