I have defined the image tag with a background color so that if there is no image the color should show but still the broken image icon is showing how to remove that ?
I dont want to make the page bulky by adding jquery or any framework , also i cant add any kind of divs to images because the site is almost done.
Found the answer with only javascript
function ImgError(source){
source.src = "/images/noimage.gif";
source.onerror = "";
return true;
}
<img src="someimage.png" onerror="ImgError(this);"/>
An image placeholder is a dummy image designed to draw attention to the need for an actual image. Wikipedia image placeholders were meant to be used on articles, especially those of living people, for the purpose of trying to obtain a freely-licensed image for them.
A placeholder is no more than an insertion point (a tag) on a page template (see Page Templates) to identify where there is a contribution region (that is, editable area) on the web page.
you can hide that using following:
<img
src="my.png"
onerror="this.style.display='none'"
/>
you can display another image if image not found as follow:
<img src="my.png" onerror="this.src = 'image-not-found.png';" />
Solution with 1x1 empty png
function ImgError(source){
empty1x1png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQI12NgYAAAAAMAASDVlMcAAAAASUVORK5CYII=";
source.src = "data:image/png;base64," + empty1x1png;
source.onerror = "";
return true;
}
<img src="someimage.png" onerror="ImgError(this);"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With