I was using something like this:
$(document).ready(function() {
$('#my-img').load(function() {
// do something
});
});
But sometimes it fails to execute the second callback (without throwing any errors, so there’s nothing to do there), and I’m thinking that maybe the image is being loaded before the document is ready.
If I don’t use the $(document).ready() part, it works fine, so I think I’m gonna leave it that way for now. But someone told me that it was a good practice to always do this kind of thing as a callback on document ready, because the document might not be ready. Is that right?
Any thoughts?
Taken from the documentation on load()
Especially the latter is a common problem.
You might try the imagesLoaded plugin, but I’ve had better luck with the following approach:
It’s dirty, but if you really need to perform some action after the image has loaded this has been the only reliable approach I’ve been able to get to work.