Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image Placeholder

Tags:

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);"/>
like image 822
Pradyumna Challa Avatar asked Jan 24 '12 13:01

Pradyumna Challa


People also ask

What is the placeholder image?

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.

What is a placeholder in design?

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.


2 Answers

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';" />
like image 172
Hemant Metalia Avatar answered Oct 02 '22 12:10

Hemant Metalia


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);"/>
like image 34
DDS Avatar answered Oct 02 '22 12:10

DDS