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.

0

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Note: window.load fires first and then document.ready after the content finishes loading.
Correcting: document.ready fires when all HTML has fineshed loading, but before the content, and window.load fires after the content finishes loading
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();

Comments

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.

3 Comments

For jquery versions >3, .load(), .unload(), and .error() removed, <script type="text/javascript"> $(window).on("load", function (e) { $(".pageloader").fadeOut("slow"); }); </script>
Thanks @mightyteja for providing details for updated Jquery version.
This is not working if I open link in a new tab and visit that tab after sometime. If you have any solution please let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.