Skip to main content

Preload Images Using Javascript

Preload Image is technique which helps browser to render images with minimum delay.

preload images

Today we will see example of image gallery. Gallery contain 5 images which load images one after another whenever user clicks next and back button.

This is a basic image gallery scenario which is used in all possible website, then why we need preloaded images for this gallery?

Now a day’s most of website becoming faster and user expectation is increasing.

Suppose your website doesn’t use preload image technique for gallery and a user visits any image gallery from Google Plus or Facebook and visits your website gallery.

Then that user always prefer those websites rather than yours. Why?

Because your website load one image at a time. When user click on next button, then only gallery load image and user has wait till it get loaded.

To avoid this situation gallery needs to download all images first so that browser cached them and when user clicks next button image get fetched from browser cache rather than image server.

Now let look at some programming related stuff

// first we create JSON record of images
var imageRecord = ['image1.jpg',

// now iterate each JSON record to load image
for (var pos = 0; pos < imageRecord.length; pos++) {

    // create image object
    var image = new Image();

    // assign image path
    image.src = imageRecord[pos];

    // additional stuff
    // some time we need to perform some logic after image load
    // it is hard to remember which image is loaded in a loop 
    // therefore we assign attribute to image which identifies JSON record of that image
    image.setAttribute('img-pos', pos);
    // bind load event to image
    Image.onload = function() {

        // get image position

        // perform operation based on fetch image index
Browser downloads images (other static resource) parallel per hostname. Therefore we can’t predict which image get downloaded first. That is why we added the image position attribute to identify which image is loaded.

If you have only few images then you can use following code

<img src = “image 1” width=”1” height=”1” />
<img src = “image 2” width=”1” height=”1” />
<img src = “image 3” width=”1” height=”1” />

As it occupy only small part of browser and all images get downloaded and cached.

Hope this tutorial post will help you.


Popular posts from this blog

AngularJs Tutorial: Getting Started

This tutorial will help you to create a dynamic web application using power of AngularJs. ·   What is AngularJs AngularJs is javascript MVC framework (maintained by Google) which molds static HTML application to dynamic web application. That was a brief introduction, you can find more information on Wiki and AngularJS Site . AngularJs follows MVC pattern to separate out model from view and manage it by controller. See this image Goal of using AngularJs is to convert static HTML template to dynamic (Angular) template.   ·   How to convert static template to dynamic (Angular) Template Now consider the following static HTML template which contain list of movies title. <html> <head></head> <body>   <h1>Upcoming Movies</h1> <ul>     <li>       <span>Godzilla</span>     </li> <li>       <span>Batman vs Superman</span>     </li> <li>       <span>Sta

Abstract Factory design pattern analysis

What is Abstract Factory design pattern?     The intent or definition of Abstract Factory is “ Provide an interface for creating families of related or dependent objects without specifying their concrete classes ” It is kind of an obscure definition, in order to understand it we need to break it into meaningful pieces. 1.  " Provide an interface "  When we talk about Abstract Factory we talk about interface not abstract class , Basically this design pattern gives us an Interface, like Interface in Java, C# , Typescript (abstract class).    Why interface?  Whenever we talk about any design pattern, it emphasizes more on how we are going to use it rather than implement it. Interface is always the right type to prefer because concrete class creates tight coupling whereas an interface isolates it from the rest of the system. At runtime we can get different solution, without updating our code. For example interface SecurityFactory

AngularJs Tutorials : Bootstrap

Every application starts with bootstrap process which initialize application and wire it other with dependencies and configurations. AngularJs is not different from other application. It also starts application with bootstrap process. Following operation happens inside AngularJs bootstrap process. Load application module. Create dependency injector and load dependencies. Compile HTML and create scope for application. All these steps happens inside angular.js scripting file. therefore we need to include it first. we can include it inside HEAD tag or at end of BODY tag. Note: Adding angular.js file at end of body tag will allow browser to load of HTML elements without any delay and afterwards load angular.js and begin bootstrapping process. You can get angular.js source file from <script src= ”” type= ”text/javascript” > AngularJs bootstrap process happens on docum