18

I have a very complex page with a lot of scripts and a rather long loading time. On top of that page I want to implement the jquery Nivo Slider (http://nivo.dev7studios.com/).

In the documentation it says I have to list all images for the slider inside of a div#slider

<div id="slider">
    <img src="images/slide1.jpg" alt="" />
    <a href="http://dev7studios.com"><img src="images/slide2.jpg" alt="" title="#htmlcaption" /></a>
    <img src="images/slide3.jpg" alt="" title="This is an example of a caption" />
    <img src="images/slide4.jpg" alt="" />
</div>

However I might have 10 images with a 1000x400px which is quite big. Those images would load when the page loads. Since they are in my header this might take quite a while.

I looking for a way to use any jquery Slider Plugin (like the nivo slider) but either dynamically load images or load all those images after everything else on my page has loaded.

Any idea how I could solve that?

Is there even a way to start a javascript process after everything else on the page has loaded? If there is a way I might have an solution for my problem (using the jquery ajax load() method) ... However I have no idea how to wait for everything else to load and then start the slider with all the images.

peterh
  • 11,875
  • 18
  • 85
  • 108
matt
  • 42,713
  • 103
  • 264
  • 397

6 Answers6

39

Here's what we did and its working great. We skipped setting src attribute of img and added img-location to a fake attribute lsrc. Then we load a dynamic image with lsrc value, and set the src of actual image only after its loaded.

Its not about faster loading, but its about showing the images only when its downloaded completely on your page, so that user do not have to see that annoying half-loaded images. A placeholder-image can be used while the actual images are being loaded.

Here's the code.

 $(function(){
    $.each(document.images, function(){
               var this_image = this;
               var src = $(this_image).attr('src') || '' ;
               if(!src.length > 0){
                   //this_image.src = options.loading; // show loading
                   var lsrc = $(this_image).attr('lsrc') || '' ;
                   if(lsrc.length > 0){
                       var img = new Image();
                       img.src = lsrc;
                       $(img).load(function() {
                           this_image.src = this.src;
                       });
                   }
               }
           });
  });

Edit: Trick is to set the src attribute only when that source is loaded in temporary img. $(img).load(fn); handles that.

simplyharsh
  • 35,488
  • 12
  • 65
  • 73
  • 6
    How come you didn't just do it like this? $('img').each(function(){ $(this).attr('src', $(this).attr('lsrc')); }); Is your way faster perhaps? – Drew Baker Aug 16 '11 at 03:22
  • 1
    @DrewBaker The question is not about faster loading, its about showing the images only when its downloaded completely on your page, so that user donot have to see that annoying half-loaded images. – simplyharsh Jun 14 '12 at 09:22
  • @simplyharsh Just so I'm sure I understood. This means `$(window).load();` will still have to wait for the images to get loaded before it fires? – Fernando Silva Feb 28 '14 at 23:10
  • 4
    @FernandoSilva No, that is the part of the trick. Window-load won't wait, because it will look for `src` attribute in `img` tag. `img` tag with fake `lsrc` attribute, means nothing on load bindings. – simplyharsh Mar 01 '14 at 14:07
  • @simplyharsh I see now, so `src` would be the perfect place to setup a loader gif... Thks – Fernando Silva Mar 01 '14 at 14:33
  • Comment take from a NAA of Juan Hdz: *@simplyharsh I have a little bug in if(!src.length > 0){ this condition always give me a false, my solution is change the condition if(src.length > 0)* – bummi Jun 21 '15 at 08:26
  • dont forget to put them all inside window.load – funky-nd Jan 14 '17 at 09:53
  • Briiiiiiliante and elegant solution, congrats! My page load time took 10s now is 2.8s – Eduardo de Souza Jul 21 '18 at 05:47
  • 1
    I'd suggest you use proper attribute for saving data, eg: "data-source" or similar, instead of lsrc. Just to follow HTML5 practice, nothing else. – Milos Radojevic Sep 12 '18 at 21:15
4

In addition to Xhalent's answer, use the .append() function in jQuery to add them to the DOM:

Your HTML would just have:

<div id="slider">
</div>

And then your jquery would be:

jQuery(function(){
    $("#slider").append('<img src="images/slide1.jpg" alt="" />');
});
Matt Asbury
  • 5,644
  • 2
  • 21
  • 29
1

check out jquery load() event, it waits for everything including graphics

$(window).load(function () {
  // run code
});

on load you could then load the images using:

var image = new Image();
image.src = "/path/to/huge/file.jpg";

You can add a function onload to the image too

image.onload = function() {
  ...
}
Han Dijk
  • 1,602
  • 1
  • 14
  • 20
0

I am using the below to power my slider and improve the page load performance.

   for (var i = document.images.length - 1; i >= 0; i--) {
      var this_image = document.images[i];
      var src = $(this_image).attr('src') || '' ;
      if(!src.length > 0){
        var lsrc = $(this_image).attr('lsrc') || '' ;
        if(lsrc.length > 0){
          $(this_image).attr("src",lsrc);
        }
      }
    }
-1

the best way to use is b -lazy js. bLazy is a lightweight lazy loading image script (less than 1.2KB minified and gzipped). It lets you lazy load and multi-serve your images so you can save bandwidth and server requests. The user will have faster load times and save data loaded if he/she doesn't browse the whole page. For a full list of options, functions and examples go to the blog post: http://dinbror.dk/blog/blazy.

The following example is a lazy loading multi-serving responsive images example with a image callback :) If your device width is smaller than 420 px it'll serve a lighter and smaller version of the image. When an image has loaded it removes the loader in the callback.

In Html

 <img class="b-lazy"
     src="placeholder-image.jpg"
     data-src="image.jpg"
     data-src-small="small-image.jpg"
     alt="Image description" />

In js

var bLazy = new Blazy({
        breakpoints: [{
        width: 420 // Max-width
          , src: 'data-src-small'
    }]
      , success: function(element){
        setTimeout(function(){
        // We want to remove the loader gif now.
        // First we find the parent container
        // then we remove the "loading" class which holds the loader image
        var parent = element.parentNode;
        parent.className = parent.className.replace(/\bloading\b/,'');
        }, 200);
        }
   });

Example

TarangP
  • 2,711
  • 5
  • 20
  • 41
-2

jquery has a syntax for executing javascript after document has loaded:

<script type="text/javascript">
jQuery(function(){

//your function implementation here...

});
</script>
Xhalent
  • 3,914
  • 22
  • 21
  • 2
    This is not correct because this function is bound to DOM ready event not window load. Window load event is the one that tells you that everything else is loaded. – Vytautas Butkus Feb 05 '13 at 10:20