Use the load() (docs) method of the window .
$(window).load(function() {
Or just do it right through window.onload .
window.onload = function() {
If you want to create a separate event for each image, put .load() on each image.
$(function() { $('img').one('load',function() {
Or, if there is a chance that the image can be cached, do the following:
$(function() { function imageLoaded() { // function to invoke for loaded image } $('img').each(function() { if( this.complete ) { imageLoaded.call( this ); } else { $(this).one('load', imageLoaded); } }); });
EDIT:
To perform an action after loading the last image, use the counter installed on the total number of images and decrease it each time the load handler is called.
When it reaches 0 , run another 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); } }); });
user113716 Feb 01 2018-11-11T00: 00Z
source share