Web Developer's Journal internet.com
side nav bar

Javascript Image Object

Spicing Up Images With Javascript

by Rodney Chang July 1, 1997


Back To The Java Bar

Rodney Chang is a co-owner of meep! media inc., an Internet and Intranet consulting company. He is also one of the programmers, specializing in CGI (Perl), Javascript and database integration. Check out some more of his articles on meep! media's online publication Web Tech.
I'm noticing more sites using Javascript to enhance their content, so I thought it'd be nice to further explore the language and its capabilities. One feature that is worth looking at is the Image object, which was introduced in the Netscape 3 browser. If you took a closer at some of the more popular interactive sites, you'll find that they use Image to the full extent. In this article we'll show you how to use the Image object and show you a few things that you can do with it.

Check The Browser First!

Before we begin, please note that our examples only work with Netscape 3 and compatible browsers, so Internet Explorer 3 users are out of luck. This also means that we'll have to put in the appropriate browser checking code. The function that handles this check is called check_browser() and the source is:

// this function checks that the browser is capable of displaying
// the button animation. Only compatible versions of netscape 3 or
// higher browsers can support it the function returns true if it's
// the correct version, and false otherwise.
function check_browser()
{
// check to see it's netscape compatible
if (navigator.userAgent.indexOf("Mozilla") == -1) {
return false;
}

// get the major version number
var version_number = navigator.userAgent.charAt(8);

// check that major version number is greater than 3
if (version_number < 3) {
return false;
}

return true;
}

As you see, the check is pretty straightforward. It returns true if it's an acceptable browser version, and false if not. As there are still some differences between Netscape Navigator and Microsoft Internet Explorer in terms of Javascript implementation, you should use this code (or a modified version of it) to make sure that your Javascript runs on both browsers.

Okay, now that we know that we have the right browser, let's look more closely at the Image object. By adding the NAME parameter to any <IMG> tag, you will be able to access the image and change its parameters. The most common way is to change the .src parameter of the image object. The browser will display whatever image is specified by the .src parameter, so we'll explore some of the possibilities.

Simple Uses

One practical use of the Image object is to make a button appear highlighted when the mouse cursor is moved over the button like this: simple Javascript highlight button example The code is very simple and is embedded inside the <A HREF> tag:

<A HREF="" OnMouseOver="test1.src='highlighted-button.gif'" OnMouseOut="test1.src='regular-button.gif'">
<IMG SRC="regular-button.gif" WIDTH=25 HEIGHT=25 BORDER=0 NAME="test1">
</A>

Simply by changing the .src parameter whenever either an OnMouseOver or OnMouseOut event occurs the image will change accordingly. Also notice that we gave the image a name called test1. Only after you give the image a name will you be able to manipulate it.

Javanimation

We can also create animations that function like GIF89 images. By using Javascript to create the animations it gives you greater control over how you want the animation displayed. Some people find the simple looping animations to become annoying after awhile, so you could limit the animation to be triggered on demand. For example, we'll take the red button and give it 3 different animations like so:

  1. When user moves the cursor over the graphic (an OnMouseOver event), a red button appears.
  2. On an OnMouseOut event the red button disappears again.
  3. After the user clicks on the button, it triggers the OnClick event and the red button lights up before performing an action.

Javascript test button Try out the animation with the button you see on the left. For this example we create an Image array populated with each frame of the animation. Then we cycle through the array to get the animated effect. Go ahead and download the source to this animation.

Banners sans CGI

The Image object also lets us to use client-side rotating banner ads. This is especially useful for those sites that don't have CGI access. The idea is to change the image after a certain time period has elapsed. To do this we call a function that will rotate the image, and then calls the SetTimeout function. SetTimeOut will sleep for the specified time and then call the rotate function again, thus creating an infinite loop.

We also add a little more functionality to it, thus if the user moves the cursor over the image, it won't be rotated until the cursor moves away. And if the user clicks on the image, the browser goes to the URL that is associated with the image. Below you'll find a sample banner ad, and check out the source code too.

Javascript Banner

This is just a simple demonstration of the variety of ways to take advantage of the Image object. Try experimenting with the simple examples first, and once you're comfortable with it then you'll be able to see the wide range of possibilities available to you. I am also collecting links that demonstrate cool ways of using Image. If you come across one that is interesting or you've made one that you think kicks serious booties, send me an email and I'll include it on this page.

For more information check out these sites:

Back to the Web Developer's Journal
Contact WDJ   •    Suits!   •    Propheads!   •    Ponytails!
Discuss   •    Subscribe   •    Search

internet.com
Internet News   Internet Investing   Internet Technology   Windows Internet Tech.   Linux/Open Source   Web Developer   ECommerce/Marketing   ISP Resources   ASP Resources   Wireless Internet   Downloads   Internet Resources   Internet Lists   International      Search internet.com   Advertising Info   Corporate Info  
internet.commerce
Be a Commerce Partner                                 
  

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers