3

I'm not talking about how to pre-load images using Javascript, I am thinking more along the lines of a Flash preloader which displays some sort of feedback while the SWF loads.

The site in question has heavy Javascript usage and requires many large images at page load so I wish to hide the site behind a loading screen till the initial images are all loaded.

Community
  • 1
  • 1
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
  • You should write a Flash preloader in (surprise!) Flash :) – David Titarenco May 04 '11 at 01:43
  • 1
    @David Obviously. But I'm not looking for a Flash preloader, I'm trying to write one in JS for a Flash-free website. – Matthew Scharley May 04 '11 at 01:44
  • Ahh, I get it. I was confuzzled. Sorry. – David Titarenco May 04 '11 at 01:48
  • Do you use jQuery? If you do, my plugin, [waitForImages](https://github.com/alexanderdickson/waitForImages) does that. – alex May 04 '11 at 01:55
  • 1
    @alex: We do, and it looks like it could definitely do what we need, so good job! One question though, does that handle CSS background images and such as well, or only `` tags? – Matthew Scharley May 04 '11 at 02:00
  • @Matthew I added it as an answer :) At the moment it only supports `img` elements, though I may have it doing `background-image` tonight (if I have some time) :) – alex May 04 '11 at 02:03
  • @alex: I'm happy to contribute that patch too if you'd rather, I've looked at the code and it's not terribly complex. – Matthew Scharley May 04 '11 at 02:04
  • @Matthew Sure, if you'd like to :) Feel free to [contact me](http://www.alexanderdickson.com/contact/) to discuss anything. – alex May 04 '11 at 02:05
  • @alex It'll be a week or two till I get to it though, I'm doing preemptive research at the moment. – Matthew Scharley May 04 '11 at 02:06
  • @Matthew I'll post a follow up here if I get around to the new feature (or I see you are following the project now too, cheers). – alex May 04 '11 at 02:08
  • 1
    @Matthew I just pushed my new version which supports image references in the CSS. Let me know what you think :) – alex May 05 '11 at 13:33

2 Answers2

3

I wrote a jQuery plugin called waitForImages that lets you do this.

The callbacks allow you to do whatever when each image has loaded...

$('body').waitForImages(
function() {
   // Called when all images have loaded.
},
function(loaded, total, success) {
   // Called once each individual image has loaded.
   // `loaded` is the number of images loaded so far.
   // `total` is the total number of images to load.
   // `success` is `true` if the image loaded and `false` if the image failed to load.
   // `this` points to the native DOM `img` element.
},
// Set the third argument to `true` if you'd like the plugin to look in the CSS
// for references to images.
true
);

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
0

I have one written when I first learned JavaScript. I'm going to try to find it in a second. The basic idea is to have a hidden element that's outside the page, and load your image in there.

Beware, ugly code as I wrote this when i started. Also it probably is not exactly what you're looking for, though there are good comments. Just modify it and make it a generic function. Based on jQuery, for a javascript gallery:

this.preload = function(){
    /*
     * Preloads all the image to a hidden div so the animation won't glitch.
     */
    if (document.getElementById("preload")){                // Checks for existance.
        var preload = document.getElementById("preload");   // Gets the preload div if it exists.
    } else {
        var preload = document.createElement("preload");    // Creates the preload div if it doesn't exist.
        $(preload).attr("id", "preload");
    }
    for (var i=0; i<this.aNodes.length; i++){               // Get all the image links
        var img = document.createElement("img");            // Loads all the image in a hidden div.
        $(img).attr("src", this.aNodes[i].href);
        preload.appendChild(img);
    }
}
Pwnna
  • 9,178
  • 21
  • 65
  • 91
  • How do you tell when it's completed loading though? I haven't done too much research into this, but I wasn't aware you could hook into that. – Matthew Scharley May 04 '11 at 01:47
  • I'm not sure, but maybe for the above, you can add something to img.onload where it would notify somewhere that image x has been loaded. Once all your desired images are loaded, you can then emit an all ready signal. – Pwnna May 04 '11 at 01:51