spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column1


Creating an Image Object

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Creating an Image object is as simple as creating any other object in JavaScript:

var variableName = new Image();

The following statement preloads the desired image:

variableName.src = "imageURL.gif";

Notice the use of the src property in the preceding statement. It enables you to associate an actual image with an instance of the Image object.

A JavaScript rollover requires at least two images. For example, take a look at the following images:


<IMG SRC="advann.gif" HEIGHT="24" WIDTH="120" NAME="advan">

<IMG SRC="advana.gif" HEIGHT="24" WIDTH="120" NAME="advan">

Note that the active and inactive images in a rollover must both be the same size. Otherwise, the active image is automatically adjusted to the size of the inactive image.

The following script shows how to combine these two images into an appealing JavaScript rollover:

if (document.images) {
  var advanoff = new Image(); // for the inactive image
  advanoff.src = "advann.gif";
  var advanon = new Image(); // for the active image
  advanon.src = "advana.gif";
}

function act() {
  if (document.images)
    document.images.advan.src = advanon.src;
}

function inact() {
  if (document.images)
    document.images.advan.src = advanoff.src;
}

The corresponding HTML code for this rollover is:

<A HREF="advantages.html"
   onMouseOver="act()" onMouseOut="inact()">
<IMG SRC="advann.gif" HEIGHT="24" WIDTH="120" 
    NAME="advan" BORDER="0" ALT="Netscent Advantages"></A>

Here's the result:

Netscent Advantages

Try placing the mouse over the image. If you click it, this page will reload, because the URLs in our example aren't real.

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


Created: August 21, 1997
Revised: March 30, 1998

URL: http://www.webreference.com/js/column1/create.html