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:
How can I fire an event when all images in the DOM are loaded?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the
load()(docs) method against thewindow.Note: On newer jQuery versions use
$(window).on('load', function(){ ...});Or just do it directly via
window.onload.If you want a separate event to fire for each image, place a
.load()on each image.Or if there’s a chance an image may be cached, do this:
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.