-1

When the server cannot find the image requested it sends a 404 and replaces it with a standard fallback image like this:

<img src="fallback.jpg"/>

I was wondering if it was possible that you could substitute another image for the fallback image.

mathmaniac88
  • 630
  • 7
  • 22

2 Answers2

1

Do you mean like a fallback image? If so consider this HTML solution

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en"> 
    <head> 
        <title>Object Test</title>
     </head> 
    <body> 
        <object data="http://stackoverflow.com/does-not-exist.png" type="image/png"> 
            <img src="https://appharbor.com/assets/images/stackoverflow-logo.png" />
        </object> 
    </body> 
</html>
James
  • 11
  • 2
0

A simple JS solution that iterates through all images on the page would be as follows:

// Get container of all img elements on page
var images = document.getElementsByTagName('img');

// Iterate through images
for (var i = 0; i < images.length; i++) {

    // Add event handler on image error
    images[i].addEventListener('error', function(e) {

        // Update SRC to custom fallback image
        e.target.src = "http://via.placeholder.com/150x150";

    });
}
Matt Pilz
  • 141
  • 5