Search code examples
javascripthtmlimageplaceholder

How to prevent the display of image placeholder if the image is not found?


Is there a way to not display the image placeholder if the image was not found? I've got images loaded by automatic feed, and for some items images are there, but for some there are not. So, if the image is not there, I would like to display a custom placeholder.


Solution

  • I found an answer here

    This piece of code works great!

    function onImgErrorSmall(source)
    {
    source.src = "/images/no-image-100px.gif";
    // disable onerror to prevent endless loop
    source.onerror = "";
    return true;
    }
    

    And then call it like this:

    <img src="/images/19013_small.jpg" alt="" onerror="onImgErrorSmall(this)" />