Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up page load by deferring images

I am creating a page which will contain a lot of large sized images, so naturally I want to make sure the page loads without too much trouble. I read this article here http://24ways.org/2010/speed-up-your-site-with-delayed-content

The method of deferring is as follows (pulled from page, don't mind the URL)

<div>     <h4>         <a href="http://allinthehead.com/" data-gravatar-hash="13734b0cb20708f79e730809c29c3c48">             Drew McLellan         </a>     </h4> </div> 

then later a snippet of js takes care of the image loading

$(window).load(function() {     $('a[data-gravatar-hash]').prepend(function(index){         var hash = $(this).attr('data-gravatar-hash')         return '<img width="100" height="100" alt="" src="http://www.gravatar.com/avatar.php?size=100&amp;gravatar_id=' + hash + '">'     }); }); 

I don't plan on doing this for every image but definitely for some image which I don't need it to show up at page load time.

Is this the best way to go or are there better ways to achieve faster page load by deferring images?

Thanks

like image 884
Huangism Avatar asked Sep 12 '12 20:09

Huangism


People also ask

How do you defer a picture load?

The main way to go to defer images is lazy loading. Thanks to lazy loading, all the images above the fold won't be loaded until the user scrolls down the page.

What does it mean to defer offscreen images?

Overview. Deferring offscreen images (or lazy-loading) refers to the practice of delaying the loading of images that aren't in your visitors' viewport until they are needed.

How do I resolve defer off screen images?

To fix 'defer offscreen images' you will need to lazy load images that are not in the visible viewport. You can do this by adding native lazy load of a lazy loading libraby. To add native lazy load just add 'loading="lazy" to the image element'.


2 Answers

A little late, but in case it benefits others, there is a great article on this topic by Patrick Sexton https://varvy.com/pagespeed/defer-images.html

He basically is suggesting the same thing, only by using tiny base 64 encoded images, he can place his image tags directly in the HTML which has the benefit of being able to control attributes like height, width, alt, etc individually. It will be a lot easier to maintain your HTML this way as opposed to creating the entire image tag in a script.

<img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" data-src="image1.jpg" alt="image 1"> <img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" data-src="image2.jpg" alt="image 2"> 

Then your script is simple and generic for all images

<script> function init() {   var imgDefer = document.getElementsByTagName('img');   for (var i = 0; i < imgDefer.length; i++) {     if (imgDefer[i].getAttribute('data-src')) {       imgDefer[i].setAttribute('src',imgDefer[i].getAttribute('data-src'));     }   } }  window.onload = init; </script> 
like image 120
jsolis Avatar answered Sep 22 '22 08:09

jsolis


This seems to be pretty clean way of deferring images. The only potential problem is if images carry important information as "Data attributes are a new feature in HTML5".

Another option could be to put images to end of body and use CSS to position them. Personally I would stick to javascript.

like image 29
nrodic Avatar answered Sep 21 '22 08:09

nrodic