Jquery check if image is uploaded

Possible duplicate:
jQuery callback when loading an image (even when caching an image)

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

Thank.

+5
source share
3 answers

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

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

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

Here is a demo

+6
source

@Scoobler , .

$(function ()
{
    $('img').error(function()
    {
        $(this).attr('src', 'http://i.stack.imgur.com/THJzu.gif');
    });
});

.error() . .

: http://jsfiddle.net/mattball/EebmC/

+6

yes, you can use an event errorlike this ....

$('#imageId')
  .error(function() {
    alert('Something bad happened.')
  })
  .attr("src", "image.gif");
0
source

All Articles