Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery check if image is loaded [duplicate]

Tags:

jquery

Possible Duplicate:
jQuery callback on image load (even when the image is cached)

Is there a way to check if image is loaded by jquery? I have some images with external src and sometime src points to 404 page. Is there a way to check if that image is loaded? and then I can remove it from dom otherwise.

Thanks.

like image 745
user558134 Avatar asked May 20 '11 08:05

user558134


1 Answers

jQuery has a function to deal with this, take a look at .error()

So for example you could attach an .error() handler to all images and display something else if there is an error, like the source no longer exists:

$('img').error(function() {
    $(this).hide();
}).attr("src", "missing.jpg");

Here is a demo

like image 62
Scoobler Avatar answered Nov 07 '22 01:11

Scoobler