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

My CMS creates img src = "(unknown)" if the image is not specified in the database. I would like to use javascript when this happens to change it to img src = "/images/missing.png" As a full JS noob, I tried several hacks, but no one seems to work ... any ideas? (there can be more than one image per page, if that matters)

+4
source share
2 answers

This should work for you if you use an event onErrorto handle a missing image.src

<img src="main_img.png" onError="this.onerror=null;this.src='/images/missing.png';" />
+7
source

src , .

if (imgSrc === "unknown") {
    imgSrc = "placeholder.png";
}
-1

Source: https://habr.com/ru/post/1670448/


All Articles