Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  78] [ 2]  / answers: 1 / hits: 45753  / 13 Years ago, tue, january 24, 2012, 12:00:00

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);/>

More From » html

 Answers
21

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'; />

[#87834] Sunday, January 22, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nia

Total Points: 461
Total Questions: 97
Total Answers: 93

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
;