views:

5842

answers:

7

I have a web page that includes a bunch of images. Sometimes the image isn't available so a broken image is displayed in the clients browser.

How do I use jQuery to get the set of images, filter it to broken images then replace the src?

--I thought it would be easier to do this with Jquery, but It turned out much easier to just use a pure javascript solution. i.e the one provided by Prestaul

+18  A: 

Basically you want to handle the onError event for the image to reassign the source. This can be done without jQuery:

function ImgError(source){
    source.src = "/images/noimage.gif";
    source.onerror = "";
    return true;
}

<img src="someimage.png" onerror="ImgError(this);"/>
Prestaul
You probably want to clear out onerror *before* setting src. Otherwise if noimage.gif is also missing you might end up with a "stack overflow".
pcorcoran
redsquare
redsquare... they are perfectly understandable when you are judicious about keeping them under control and can even be useful when you want your markup to reflect what is actually going to happen.
Renesis
+10  A: 

I believe this is what you're after: jQuery.Preload

Here's the example code from the demo, you specify the loading and not found images and you're all set:

$('#images img').preload({
    placeholder:'placeholder.jpg',
    notFound:'notfound.jpg'
});
Nick Craver
jQuery.Preload is a plugin which is required to use the example code.
Dan Lord
Yes...this is linked in the answer on the first line.
Nick Craver
A: 

Not sure if there is a better way, but I can think of a hack to get it - you could ajax post to the img url, and parse the response to see if the image actually came back. If it came back as a 404 or something, then swap out the img. Though i expect this to be quite slow.

Chii
+1  A: 

I don't know jQuery yet, so my answer will be generic (and the result of a quick search...). I found the page Detecting broken images with JavaScript (via a DZone Snippet, but I better give the original source!) which gives a simple and apparently relatively cross-browser (to test on Opera/Safari) method.

Of course, it would be better to serve a non-broken page, no? Although to be honest it can be a connection issue to.

PhiLho
+6  A: 

Here is a standalone solution:

$(window).load(function() {
  $('img').each(function() {
    if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
      // image was broken, replace with your new image
      this.src = 'http://www.tranism.com/weblog/images/broken_ipod.gif';
    }
  });
});
Devon
Sugendran
+8  A: 

I use the built in error handler:

$("img").error(function () {
  $(this).unbind("error").attr("src", "broken.gif");
});
travis
+1  A: 
$(window).bind('load', function() {
$('img').each(function() {
    if((typeof this.naturalWidth != "undefined" &&
        this.naturalWidth == 0 ) 
        || this.readyState == 'uninitialized' ) {
        $(this).attr('src', 'missing.jpg');
    }
}); })

Source: http://www.insideria.com/2009/03/jquery-quickie---broken-images.html

Mohamad