header.gif (6771 bytes)

Lesson 9-Images & JavaScript

The image object is very important in JavaScript. This object allows us to access images using JavaScript. Ok, in this section, we shall discuss:

Red_CurlyC035.gif (285 bytes) Accessing images using JavaScript

The image object, as mentioned, is created for you whenever you insert images onto your webpage. It does so automatically, even if there's not one line of JavaScript code on your page, as opposed to the Date object, for example. All you have to do is know how to refer to these images using JavaScript so you can manipulate them. Assuming you have three images on your webpage, as follows:

Image 1: Image 2: Image 3:
tn00607a.gif (1499 bytes) tn00738a.gif (1685 bytes) tn00897_.gif (2529 bytes)

If you want to refer to the three images using JavaScript, here's how:

document.images[0] //first image
document.images[1] //second image
document.images[2] //third image

As can be seen, images to JavaScript are an array of images, so you use "[ ]" to access each image, "[0]" being the first one. If you are not familiar with arrays, not to worry, they will be explained later on. There's an alternative to using integers to access the images, and that is using the name of each image. (Uh? what name?) Well, the name is something you can choose if you want to add to an image, which makes referring to them a lot easier. The name is inserted in the html tag. Lets use the first image as an example:

<html>
<body>

<img name="myimage01" src="whatever.gif" width="100" height="50">
</html>

Now, to access this image, this is what you'd do instead:

document.images["myimage01"] //first image

Once you can access images, you can create animations or mouse over/out, even mouse down/up effects. For example, this is a typical example of a mouseover/mouseout effect (works with Netscape 3.x and 4.x) We will later see how we can create this effect for IE4.x browsers too.

This can be done with the onmouseover, onmouseout event handlers-along with the help of preloading images. So lets first talk about images being preloaded.

Red_CurlyC035.gif (285 bytes) Preloading images

What is it? It is basically loading an image into cache before being used, so whenever we want to use it, bam! it appears instantaneously. Usually, when we load a page, all image are not preloaded-that's why we see these image becoming visible bit by bit. Why would we want to preload an image? Because it is the solution to avoiding having delays in between switches of images when using the onmouseover/onmousedown effect-among other things. How do we preload an image? By creating an instance of the image object, and inserting it in the <head></head>tags. Ok, don't worry, I'll explain it below: Since the object contains simply a few properties/methods, I'll list them ALL here:

The Image object
properties methods
src onLoad
height,width onError
lowsrc onAbort
border  
vspace,hspace  

Ok, lets preload two images to be used for the onmouseover/onmouseout effect. These are the two images we shall preload:

1.gif (1557 bytes) 3.gif (1752 bytes)
"1.gif" "3.gif"

<head>
<script>
<!--

image01= new Image(42,42)
image01.src="1.gif"
image02= new Image(42,42)
image02.src="3.gif"

//-->
</script>

</head>

You may be thinking to yourself :"What is this? This doesn't make any sense-why are we doing all this?" The answer is quite simple. In html, there simply is no way you can load an image without showing it. All image tags go within the body tag, which immediately displays it. But in JavaScript, you can-you can first load the image and defer until the right time to wam boom bala boom. So, that's why we are going through all this mess to do something like this. Don't worry, this is about all you need to know about the image object; the other properties are virtually not used at all.

Now that we've preloaded the images, and, recalling the way we accessed images (on the very top) using JavaScript, we can get down to business. The basic idea is to have JavaScript alter the path of the image depending on whether its onmouseover or onmouseout. Change the path, change the image!

Red_CurlyC035.gif (285 bytes) onMouseover/onMouseout effects script

<html>
<head>
<script language="JavaScript">
<!--
image01= new Image(42,42)
image01.src="1.gif"
image02= new Image(42,42)
image02.src="3.gif"
//-->
</script>
</head>


<body>
<a href="whatever.htm"
onmouseover=
"document.images['example'].src=image02.src"
onmouseout=
"document.images['example'].src=image01.src">

<img src="1.gif"
name="example"></a>
</body>

</html>

The above will work and produce the effect...there is one problem though: It works only with NS3.x and 4.x. It will not work with any versions of IE, and the problem is more serious than that. It will, in fact, produce error messages for IE users, either upon loading or after putting their mouse over the image. What we need here is a "filter" code that will filter out browsers that don't support the onmouseover code. So why didn't I simply implement a filter above? Because I wanted to show you exactly how the process of swapping images occurs. The concept is very important, and if I simply gave you the final version with the filter added, you may not see this. Anyhow, moving on, we will now try to implement a filter that will take care of the problem.

Lesson 10