Display Loading Image When Page Loads Using jQuery

Introduction
 
Often we build build web applications and due to some reason it takes more time to load the page. At the time of loading page, it shows blank(white page) in UI to User. Some Users seeing delay in page load, close the browser because they are not able to see anything. In order to avoid this, browser will say page is loading please wait. Here we will discuss how can we accomplish.
 
Using Code
 
Step 1: Add loader DIV tag inside body tag. This DIV helps to display the message. 
  1. <div id="loader"></div>  
Step 2: Add following CSS how it is going to displaying in browser.
  1. #loader {  
  2.     positionfixed;  
  3.     left: 0px;  
  4.     top: 0px;  
  5.     width100%;  
  6.     height100%;  
  7.     z-index9999;  
  8.     backgroundurl('pageloader.gif'50% 50% no-repeat rgb(249,249,249);  
  9. }  
Step 3: Add following jQuery code when to fadeout loading image when page loads. First code block is to add jQuery reference and second code block to fade out the div in browser once everything loaded properly. 
  1. <script src="http://code.jquery.com/jquery-1.8.2.js"></script> 

  2. <script type="text/javascript">  
  3.    $(window).load(function() {  
  4.       $("#loader").fadeOut(1000);  
  5.    });
  6. </script>  
Note: To get complete code download the attached zip file.
 
Conclusion
 
We discussed how can we show loading image when page takes more time to load. Hope this helps.