Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Preloading images

Preloading images is the processes of loading images into memory before they are needed.

The next section gives you a complete example of a fully functional image loading process. The example shows you how to preload an image then show it afterwards in a different window, which means the user does not have to wait for it load. This example only uses one image, but that's all that's needed to demonstrate the point. However the same logic can be applied to many more images. Run Code | View Code

Process

Events

Input

Event Trigger

When the user clicks of the "Prepare web application" button the preloadImage function is called which first makes an image object, sets it's src property to an existing picture then finally decides wether or not to attach the images onLoad event depending on the image's complete property. No matter what the situation the redirect function is called that redirects the user to a web page that has the preloaded image defined in it's body.

The Solution

Set up the button

Make the button and make it call the preloadImage function when the user clicks on it. This allows the user to start the process. <input type="button" value="Prepare web application" onClick="preloadImage">

Define preloadImage

Function analyses

The preload image function's purpose is to preload one image that resides on a different web page. As soon as the image is loaded the redirect function is called which sends the user to the web page that holds the image that was preloaded. That web page will show the image immediately because all image data was already downloaded to the client. The benefit of doing this is that the user does not have to wait for the image to display.

This function first declares a single but important local variable pic. This variable is then initialized with the built in Image object. Straight afterwards it's src property is set to an existing file location that points to a real image.

After initial setup the code then checks if the image was previously loaded or not by querying the complete property. If the complete property returns true it means the image is already in the user's machine. If it's false it means the image is not on the client's machine.

If the image is on the client's machine there is no need to download the image again and even if it was loaded with JavaScript it would fail since the web browser will not call the image's onLoad event. Therefore the nested redirect function is called straight away.

However if the image is not on the client's machine the onLoad event of the image is registered and forced to call the redirect function. The redirect function is called as soon as the image is loaded.

If the image does not exist the onLoad event will never be called as a circumstance the redirect function will never get called either. That's why it is important to manually verify that the image does exist before tempting to preload an image in this fashion.

Comments with code

function preloadImage(){
Holds the image object var pic Create an image object var pic = new Image() Set the image object's src property to an existing image pic.src = "images/pl1.jpg" Has this image been previously loaded? if(pic.complete){
Call the redirect function immediately since the image was loaded before redirect()
The image has changed or it was never loaded }else{
Make the onLoad event of the image call the redirect function as soon as all image data for the image is received pic.onload = redirect
} Define the redirect function function redirect(){
Change the URL of the current window to a new file.
This file contains an image tag that holds the loaded image window.location="image_preload_win.htm"
}
}

Importent

Run Code | View Code

See also: Making a rollover | Making windows


Using alert | Using the status bar | Changing the background color of the document | Changing the content of a document | Using prompt | Using confirm | Changing the URL of a window Manually | Querying a text field's value | Checking the selected value of a select object | Checking if a checkbox is selected | Checking if a radio button is selected in a group | Making a rollover | Getting the time | Using setTimeout/setInterval | Making windows | Making cookies

JavaScript Tutorial Home