Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If image 'src' is '(unknown)' replace with 'missing.png'

My CMS produces an img src="(unknown)" if an image isn't provided in the database. I would like to use javascript when this occurs to change it to img src="/images/missing.png" As a complete JS noob I have tried several hacks but none seem to work... any ideas? (there is likely to be more than one image on the page if that makes any difference)

like image 555
Hoobamac Avatar asked Feb 21 '17 17:02

Hoobamac


1 Answers

This should work for you, if you use onError event for handling missing image src

<img src="main_img.png" onError="this.onerror=null;this.src='/images/missing.png';" />
like image 101
Always Sunny Avatar answered Oct 19 '22 05:10

Always Sunny