spacer

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

home / experts / javascript / column1


Creating an Image Object

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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

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