Ahmad Fouad Ahmad Fouad - 1 year ago 65
Ajax Question

Is it possible to preload page contents with ajax/jquery technique?

Is it possible to preload all page contents (like showing a loading bar / animated gif.. or loading text.. ) until the contents are fully loaded and then displayed to the user/visitor ? If this is possible, can you give me just directions or resources to follow to achieve this. Because I was able to find image preloaders easily, but I am seeking for a preloading technique that will preload all content on the page before being displayed.

Answer Source

There's no need to use Ajax for this. Simply set the page's wrapper div display to none. Then change it to block when the document is loaded.

Your code might look like this, using vanilla javascript:

<script type="text/javascript">
    function preloader() {
        document.getElementById("preloader").style.display = "none";
        document.getElementById("container").style.display = "block";

    window.onload = preloader;

div#wrapper {
    display: none;

div#preloader {             
    top: 0; right: 10px;
    width: 132px; height: 38px;
    background: url(path/to/preloaderBg.png) no-repeat;
    cursor: wait;
    text-shadow: 0px 1px 0px #fefefe;  //webkit                 

    <div id="preloader">Loading... Please Wait.</div>
    <div id="wrapper">
        <!-- page contents goes here -->

Update, in jQuery:

<script type="text/javascript">
    // Use $(window).load(fn) if you need to load "all" page content including images, frames, etc.

Related documents: Events/ready, Events/load, CSS/css & Core/$

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download