I have a page that has a list of many <img>
tag. So it takes a long time to load all images. Before loading any image, I see the broken image icon. I want to replace broken image while loading the images. I tested this answer, but it just worked when a error happens. Is there anyway for doing that with javascript or jquery?
-
1Try adding with `CSS` `like img { background: url('someimage.jpg') no-repeat; }` – Guruprasad J Rao Sep 15 '15 at 07:40
-
binding function `onerror` is your best option. – rrk Sep 15 '15 at 07:41
-
@GuruprasadRa Can't do it with javascript? – hamed Sep 15 '15 at 07:41
-
1Sounds like more of a performance question. How does one load in a large amount of images. In my opinion the best solution would be to lazy load the images as the user scrolls down the page. A data attribute would contain the path to the actual image while the src attribute would contain the path to a small loading gif. As a user scrolls down and an image comes into view the data attribute would replace the loading gif with the actual image. This increase performance by not loading in all your images at once. I like http://luis-almeida.github.io/unveil/ – pizzarob Sep 15 '15 at 07:42
-
find a couple of good answers in this thread, image placeholder is a good idea: http://stackoverflow.com/questions/1970569/slow-loading-images-best-way-to-display-temporary-image-jquery-perhaps – Saar Sep 15 '15 at 07:44
-
Found one **[here too](http://stackoverflow.com/a/29111371/2065039)**, but using `js`, I ain't sure on that.. – Guruprasad J Rao Sep 15 '15 at 07:45
-
lazyload the images to reduce load and resolve this issue at the same time, i'd recommend https://github.com/aFarkas/lazysizes – Hannes Sep 15 '15 at 09:11
3 Answers
I found a good solution on GitHub. Just use the CSS code below:
img[src=""],
img:not([src]) {
visibility: hidden;
}
Link: https://github.com/wp-media/rocket-lazy-load/issues/60

- 183
- 1
- 12
You can load a placeholder image, but then you must load that image (when you're already loading another image). If you load something like a spinner via a GET
request, that should be ok since you can set cache headers from the server so the browser does not actually make any additional requests for that loading image. A way that Pinterest gets around this is by loading a solid color and the title of each of their posts in the post boxes while the images are loading, but now we're getting into a design discussion. There are multiple ways to skin a cat.
Regarding loading several images, you have to understand a couple considerations:
- The time it takes to fetch and download an image.
- The time it takes to decode this image.
- The maximum number of concurrent sockets you may have open on a page.
If you don't have a ton of images that need to be loaded up front, consideration 3 is typically not a problem since you can optimistically load images under the fold, but if you have 100s of images on the page that need to be loaded quickly for a good user experience, then you may need to find a better solution. Why? Because you're incurring 100s of additional round trips to your server just load each image which makes up a small portion of the total loading spectrum (the spectrum being 100s of images). Not only that, but you're getting choked by the browser limitation of having X number of concurrent requests to fetch these images.
If you have many small images, you may want to go with an approach similar to what Dropbox describes here. The basic gist is that you make one giant request for multiple thumbnails and then get a chunked encoding response back. That means that each packet on the response will contain the payload of each thumbnail. Usually this means that you're getting back the base64-encoded version of the payload, which means that, although you are reducing the number of round trips to your server to potentially just one, you will have a greater amount of data to transfer to the client (browser) since the string representation of the payload will be larger than the binary representation. Another issue is that you can no longer safely cache this request on the browser without using something like IndexedDB. You also incur a decode cost when you set the background image of each img
tag to a base64 string since the browser now must convert the string to binary and then have the img
tag decode that as whatever file format it is (instead of skipping the base64
->binary
step altogether when you request an image and get a binary response back).

- 6,204
- 6
- 38
- 55
you can use placeholder image, which is very light weight and use that in place of each image.
same time while loading page, you can load all the images in hidden div. then on document ready you can replace all the images with jQuery.
e.g.
HTML
<img src="tiny_placeholder_image" alt="" data-src="original_image_src"/>
<!-- upto N images -->
<!-- images are loading in background -->
<div style="display:none">
<img src="original_image_src" alt=""/>
<!-- upto N images -->
</div>
JavaScript
(function($){
// Now replace all data-src with src in images.
})(jQuery);

- 5,562
- 2
- 29
- 50