4

I need a little help. I do not know where to start. I need to add a page loader to my website. I first submit a form it then uses SimpleXML to call an exterior XML Sheet with expedia... It takes a minute to load, so I would like to add the image loader to this page. But how do I go about doing this? I have looked on Google and could not find any useful info.

I need it to show the loader until the COMPLETE page has loaded.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
luv2code
  • 1,216
  • 6
  • 22
  • 42

3 Answers3

17

This has many solutions, but a simple one is to:

1- Create an overlay DIV with your loader stuff and prepend to BODY;
2- Add an event listener for window.load or document.ready event that hides this DIV.

// On the first line inside BODY tag
<script type="text/javascript">
    jQuery("body").prepend('<div id="preloader">Loading...</div>');
    jQuery(document).ready(function() {
        jQuery("#preloader").remove();
    });
</script>

(code typos fixed)

Shomz
  • 37,421
  • 4
  • 57
  • 85
Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
  • Note: `window.load` fires first and then `document.ready` after the content finishes loading. – Ricardo Souza Apr 28 '12 at 17:27
  • 2
    **Correcting:** `document.ready` fires when all HTML has fineshed loading, but before the content, and `window.load` fires after the content finishes loading – Ricardo Souza Nov 01 '12 at 14:30
4

Check out spin.js http://fgnass.github.com/spin.js/

var opts = {
lines: 13, // The number of lines to draw
length: 7, // The length of each line
width: 4, // The line thickness
radius: 10, // The radius of the inner circle
// Even more options available.... 
};
var target = document.getElementById('loading');
var spinner = new Spinner(opts).spin(target);

And once your form is done:

$("#loading").data('spinner').stop();
Connor
  • 1,024
  • 4
  • 16
  • 24
2

This loader div will show a loading image on page load.

$(window).load(function() {
  $(".pageloader").fadeOut("slow");
});
body {
  background: black;/* for this demo */
}

.pageloader {
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
  height: 100%;
  z-index: 9999;
  background: url('images/yourLoaderImage.gif') 50% 50% no-repeat rgb(249, 249, 249);
  opacity: .8;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="pageloader"></div>

It's Completely working for me.

Walf
  • 8,535
  • 2
  • 44
  • 59