tags:

views:

1169

answers:

5

Though I think it's not possible, it's always awesome to know what the SO Gurus would act if they were me. So, here's my question:

Is there any way to render a default image in an HTML <img> tag, in case the src attibute is invalid (using only HTML)? If not, what would be your lightweight way to work around it?

+1  A: 
<style type="text/css">
img {
   background-image: url('/images/default.png')
}
</style>
lambacck
+4  A: 

You asked for an HTML only solution...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd"&gt;

<html lang="en">
<head>
<title>Object Test</title>
</head>
<body>   

  <object data="http://stackoverflow.com/does-not-exist.png"&gt;
    <img src="http://stackoverflow.com/content/img/so/logo.png"&gt;
  </object>

</body>
</html>

Since the first image doesn't exist, the fallback (the Stack Overflow logo) will display. And if you're using a really old browser that doesn't support object, it will just ignore that tag and use the img tag.

Patrick McElhaney
WOW! It worked! Any known compatability issues or stuff?
Galilyou
Fairly sure that this usage of the object tag has compatability issues in IE.
Svend
Hew! IE8 doesn't recognize it at all :( , instead it displays the alt text. Worked on Firefox and Chrome though. Suggestions!?
Galilyou
IE8 is working now, but only by defining the absolute path of the file of the Data property (<object Data="../../content/default.jpg/". Not sure what's wrong.
Galilyou
It worked for me in IE8 after I put in the DTD for HTML 4.01.
Patrick McElhaney
Accepted! Thanks Patrick
Galilyou
+1  A: 

Using Jquery you could do something like this:

$(document).ready(function() {
    if ($("img").attr("src") != null)
    {
       if ($("img").attr("src").toString() == "")
       {
            $("img").attr("src", "images/default.jpg");
       }
    }
    else
    {
        $("img").attr("src", "images/default.jpg");
    }
});
Jon
+3  A: 

This works well for me. Maybe you wanna use JQuery to hook the event.

<img src="foo.jpg" onerror="this.src='error.jpg';">
Svend
+1 but not Html only solution. However, I might end up using this as my solution. Do you know of any issues with your answer?
Galilyou
Using the onerror event to catch broken images is as old as ... well, it's an old technique, and as far as I know, supported in all browsers.
Svend
+1  A: 

I don't think it is possible using just HTML. However using javascript this should be doable. Bassicly we loop over each image, test if it is complete and if it's naturalWidth is zero then that means that it not found. Here is the code:

fixBrokenImages = function( url ){
    var img = document.getElementsByTagName('img');
    var i=0, l=img.length;
    for(;i<l;i++){
        var t = img[i];
        if(t.naturalWidth === 0){
            //this image is broken
            t.src = url;
        }
    }
}

Use it like this:

 window.onload = function() {
    fixBrokenImages('example.com/image.png');
 }

Tested in Chrome and Firefox

Pim Jager
It's working Pim, and it's totally reusable, but I would prefer the lightweight option for this very case. Thanks a lot :) +1
Galilyou