views:

188

answers:

1

Hello, does anybody knows how to check if an image loads properly. I have a script that preloads images and what I wanted to do is if the image doesn't exists, which causes my script not to end, I would just make the attribute "src" to be a default value

image.load(function(){
 //it never reache here if the image doesn't exist
}).attr('src','a non existing image');

Thanks

A: 
var image = $('<img>').appendTo('body');

image.load(function() {
    // image is found and loaded
}).error(function() {
    // image not loaded
    image.attr('src','default-image.jpg');
}).attr('src','a non existing image');
David
Thanks!That solved my problem.
Wolfgang