This is a solution with Pure CSS.
As far as I can say, my best bet would be, well, replacing it with an empty image placeholder with a background image inline style, that is an SVG, centred in height and width, and specified as a Data URI, as that never fails.
<img src="image-to-be-loaded.png" />
Having said that multiple images, use the following CSS:
img {
background: url('data:') center center no-repeat transparent;
background-size: cover; /* for new browsers */
}
Using the Server to handle the request.
This is like transferring the pain to the server, irrespective of the browser that is requesting the resource. You can do this in these steps:
- Redirect all the static resource requests to a router script.
- Check if the request gives a 404 error or non-image file from the script.
- If the file exists, serve the file.
- If not, grab the placeholder image and server it.
This can be easily done using PHP with the following functions:
file_exists()
: Check if the file is existing in the server or not.
file_get_contents()
: Read the file and store or spit on the browser.
I am definitely sure that this solution works in server side handling, and if you have a server side script in place, you can make use of these steps to achieve without using JavaScript.
Hope this helps.