|
|
|
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
<input type="button" value="Prepare web application" onClick="preloadImage">
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.
function preloadImage(){
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){
redirect()
}else{
pic.onload = redirect
}
Define the redirect function
function redirect(){
window.location="image_preload_win.htm"
}
}
In Internet explorer 4 and Netscape 6 and higher the "onload" event will not be called by the image if the image was previously loaded so the image's "complete" property must be verified if it's loaded or not. The complete property returns "true" if it was loaded before otherwise it returns "false". Netscape 4.5 and lower does not understand the "complete" property but the "onload" event is called wether or not the image was previously loaded.
When creating an image object and setting it's "src" property is MUST point to a real image that exists! (That's if you want an image loader function to function properly) If it does not exist the image's "onload" event will never be called and the "complete" property will never be "true". Therefore any image loader function will never know if the picture is ready or not. So if there is a problem with the loading procedure it's most likely because the supplied file location cannot be found.
In Internet explorer there is a Bug. If an alert statement is placed just before the image's onload event function register the event will never be called by the image. To avoid this bug never structure you code in the following manner:
alert("here")
image.onload = functionHandler
The best way to preload an image is to create an image object so the image can expose you to the "onload" event and "complete" property. However in same situations it is not necessary to know when an image is loaded, all that's wanted is a loaded image. To perform this one can load an image into a hidden frame , IFRAME or layer.
A web author would want to preload images in the following situations:
The main purpose for preloading images is to increase user satisfaction by speeding up download times and making it seam as though the user didn't wait for the image to load. It also makes the web application look more professional and reliable because it's interface is fully painted as soon as the user invokes the application. The strategy is to alter the user's perception so he or she believes the web application is up to standard and running at it's fullest potential.
See also: Making a rollover | Making windows