Reputation: 1803
I know this is a commonly asked question, but has anybody got a bullet proof jquery routine for checking if images load successfully, and if they don't, do something about it. I've tried a number of ideas posted on here and other forums. I've also tried a couple of plugins, imagesLoaded and waitForImages, all of which seems to have holes which mean some broken images go undetected. Can anybody advise please? Thanks.
Here's the code I'm currently using:
$(function() {
$(".vehicleImage").one("load", function() {
// Image loaded successfully
};
}).each(function() {
if (this.complete) $(this).load();
}).error(function () {
// Image didn't load, so do something about it
})
});
Upvotes: 1
Views: 277
Reputation: 339
There is a technique from Remy Sharp which works very well: https://gist.github.com/2028925
Remy creates a new Image object in JavaScript and wraps that in jQuery instead of working with an img element from the DOM.
Upvotes: 1