Quick loading image for all ajax call

We have very common need of displaying some loading animation while ajax call is going on background. There have been lots of help out there. Here is another repetitive version of it. It should work to any of the application out there.

<script language="javascript">
$('#loadingDiv')
 .hide() // hide it initially
 .ajaxStart(function() {
 $(this).show();
 })
 .ajaxStop(function() {
 $(this).hide();
 });
</script>

$('#loadImageLocation').prepend('<div 
id="loadingDiv" ><img id="theImg" 
src="<path of loading image>" /></div>');

In the above code make following changes

  1. Add a JQuery reference to your page.
  2. Change <path of loading image> to actual image url of loading animated image.

Happy JQuery!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s