Show loader image on the middle of the page on Ajax request using Jquery.
Now a days , use of Ajax in the web pages has increased subsequently to build the better user experiences. We often put the XMLHttp request (Ajax) to the server scripts to get the results from server and manipulate the results as per desired using client side scripts (javascripts) . Basically we prefer using some loader images on ajax request to notify that user request is in process. So , here I have wrote simple jquery scripts to enhance better image loader on ajax request with inbuilt UI blocker (to restrict other events during request) . If we are using huge number of ajax request then , this script can sound helpful.In this script I have used Jquery with object oriented javascript approach . I did not bother to make it as plugin , becuase its not that huge and not any setting required. ![]()
DEMO
Here is my script.
<script language="javascript" type="text/javascript"> $(document).ready(function(){ // author prabeen. $.AjaxLoader = { // public access ImagePath : "ajax-loader.gif" , // public access BlockerOpacity:0, //private access ImageObject : null, //public access Initialize : function(){ this.PreLoadImage(); }, //private access PreLoadImage: function(){ this.ImageObject = new Image(); this.ImageObject.src = this.ImagePath; this.ImageObject.Height = 50; this.ImageObject.Width = 50; }, // pulic access AppendImageObject: function(){ var _position = this.FindMiddlePosition(); $('body').append(this.ImageObject) .find("img:last") .css({'position':'absolute','top':_position[1]+'px','left':_position[0]+'px','z-index':2000}) .addClass('LoaderImage'); this.AppendBlocker(); }, //private access FindMiddlePosition:function(){ var document_height = $(document).height(); var window_width = $(window).width(); var window_height = $(window).height() ; var left = Math.round((window_width / 2)- this.ImageObject.Width); var top = Math.round((window_height / 2)- this.ImageObject.Height) + $(document).scrollTop(); return [left,top ]; }, //public access RemoveImageObject:function(){ $('img.LoaderImage').remove(); this.RemoveBlocker(); }, //private access AppendBlocker:function(){ $('body').append('<div></div>') .find(':last') .addClass('ajax_blocker') .css({'opacity':this.BlockerOpacity,'display':'block','position':'absolute','top':0,'left':0,'height':$(document).height()+'px','width':$(document).width()+'px','z-index':1000,'background-color':'white'}); }, //private access RemoveBlocker :function(){ $('div.ajax_blocker').hide().remove(); } } }); </script>
How to use this .
Lets say your Jquery ajax request script is like this
<script> $('yourElement').click(function(){ $.ajax({ type: "POST", // url of server side script url: "some.php", //your query parameters data: "name=John&location=Boston", //when ajax request is complete success: function(msg){ alert( "Data Saved: " + msg ); } }); }); </script>
Integrating above script to render ajax loader Image
Note:Make sure you have included above script and Jquery in the page .
<script> // set value for the 'ImagePath' property if you want to change the default image path i.e 'images/ajax-loader.gif' $.AjaxLoader.ImagePath = 'myimagepath.gif'; // set value for the 'BlockerOpacity' property if you want to change the default opacity of UI blocker i.e 0 (100% transparency ) .It can be between 0 to 1 $.AjaxLoader.BlockerOpacity = 0.5; // if you want to pre load the loader image otherwise it is not required. $.AjaxLoader.initialize(); $('yourElement').click(function(){ //show the loader image and UI blocker becauase we are sending the ajax request. $.AjaxLoader.AppendImageObject() $.ajax({ // request method . type: "POST", // url of server side script url: "some.php", //your query parameters data: "name=John&location=Boston", //when ajax request is complete success: function(msg){ // this method removes the loader image and UI blocker from the DOM after completion of ajax request. $.AjaxLoader.RemoveImageObject(); /* your script to manipulate ajax response. */ } }); }); </script>


The post is really helpful to those who never noticed while ajax request is being sent, no other links and buttons on the page should work…. This post will help them to notice this things, further this post will really help me I was looking for similar script.
Thanks Prabeen your post are always helpful and covers minors but most important issue of web-development………
Thank You akash…………
I like this post, enjoyed this one appreciate it for putting up.
I would say it can be my extremely 1st time viewing your web site. I can undoubtedly write that it contains a great number of helpful points. Excellent website. I just finished mine and i was looking for some ideas and you gave me a couple of. Great read!