Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery callback after all images in dom are loaded?

Tags:

jquery

image

load

How can I fire an event when all images in the DOM are loaded? I've googled a lot. I've found this, but it doesn't seem to work:

jQuery event for images loaded

like image 451
Dee Avatar asked Feb 01 '11 01:02

Dee


People also ask

Does jQuery wait for images to load?

In jQuery when you do this: $(function() { alert("DOM is loaded, but images not necessarily all loaded"); }); It waits for the DOM to load and executes your code. If all the images are not loaded then it still executes the code.

How do you check if all images are loaded Javascript?

To determine whether an image has been completely loaded, you can use the HTMLImageElement interface's complete attribute. It returns true if the image has completely loaded and false otherwise. We can use this with naturalWidth or naturalHeight properties, which would return 0 when the image failed to load.

How check image is loaded or not in jQuery?

To check if an image is loaded successful or not, you can combine the use of jQuery 'load()' and 'error()' event : $('#image1') . load(function(){ $('#result1'). text('Image is loaded!


2 Answers

Use the load()(docs) method against the window.

$(window).load(function() {     // this will fire after the entire page is loaded, including images }); 

Note: On newer jQuery versions use $(window).on('load', function(){ ...});

Or just do it directly via window.onload .

window.onload = function() {     // this will fire after the entire page is loaded, including images }; 

If you want a separate event to fire for each image, place a .load() on each image.

$(function() {     $('img').one('load',function() {         // fire when image loads     }); }); 

Or if there's a chance an image may be cached, do this:

$(function() {     function imageLoaded() {        // function to invoke for loaded image     }     $('img').each(function() {         if( this.complete ) {             imageLoaded.call( this );         } else {             $(this).one('load', imageLoaded);         }     }); }); 

EDIT:

In order to perform some action after the last image loads, use a counter set at the total number of images, and decrement each time a load handler is called.

When it reaches 0, run some other code.

$(function() {     function imageLoaded() {        // function to invoke for loaded image        // decrement the counter        counter--;         if( counter === 0 ) {            // counter is 0 which means the last            //    one loaded, so do something else        }     }     var images = $('img');     var counter = images.length;  // initialize the counter      images.each(function() {         if( this.complete ) {             imageLoaded.call( this );         } else {             $(this).one('load', imageLoaded);         }     }); }); 
like image 105
user113716 Avatar answered Sep 20 '22 17:09

user113716


Below is what I came up with, using deferred objects and $.when instead of using a counter.

var deferreds = []; $('img').each(function() {     if (!this.complete) {         var deferred = $.Deferred();         $(this).one('load', deferred.resolve);         deferreds.push(deferred);     } }); $.when.apply($, deferreds).done(function() {     /* things to do when all images loaded */ }); 

Let me know if there is any caveats.

like image 23
holmis83 Avatar answered Sep 16 '22 17:09

holmis83