Sams Teach Yourself JavaScript in 24 Hours Free Open Book

Sams Teach Yourself JavaScript in 24 Hours

Previous Page
Next Page

Using Dynamic Images

Long before the W3C DOM allowed JavaScript to change any part of a web page, a feature called dynamic images enabled you to swap one image for another with JavaScript. This technique is still supported by current browsers, and is still the most convenient (and compatible) way to work with images in JavaScript.

Working with image Objects

You can change images dynamically by using the image object associated with each one. The traditional way to do this is with the document.images array. This array contains an item for each of the images defined on the page. In the object hierarchy, each image object is a child of the document object.

With the W3C DOM, you can also assign an id attribute to an image within the <img> tag, and then use document.getElementById to find the object for that image. Each image object has the following properties:

  • complete is a flag that tells you whether the image has been completely loaded. This is a Boolean value (true or false).

  • height and width reflect the corresponding image attributes. This is for information only; you can't change an image's size dynamically.

  • hspace and vspace represent the corresponding image attributes, which define the image's placement on the page. Again, this is a read-only attribute.

  • name is the image's name. You can define this with the NAME attribute in the image definition.

  • src is the image's source, or URL. You can change this value to change images dynamically.

For most purposes, the src attribute is the only one you'll use. The image object has no methods. It does have three event handlers you can use:

  • The onLoad event occurs when the image finishes loading. (Because the onLoad event for the entire document is triggered when all images have finished loading, it's usually a better choice.)

  • The onAbort event occurs if the user aborts the page before the image is loaded.

  • The onError event occurs if the image file is not found or corrupt.

By the Way

Although changing image sources works fine, you can also use the W3C DOM to completely remove or replace image objects, or insert new ones, just like any other object.


Preloading Images

You can also create an independent image object. This enables you to specify an image that will be loaded and placed in the cache, but will not be displayed on the page.

This might sound useless, but it's a great way to work with modem-speed connections. After you've preloaded an image, you can replace any of the images on the page with that imageand because it's already cached, the change happens instantly. Even on a fast connection, this avoids flickering and makes animation smoother.

You can cache an image by creating a new image object, using the new keyword. Here's an example:

Image2 = new Image();
Image2.src = "arrow1.gif";

By the Way

You learned about the new keyword and its other uses for object-oriented programming in Hour 6, "Using Functions and Objects."


Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


     Main Menu
Sams Teach Yourself JavaScript in 24 Hours
Table of Contents
Copyright
About the Author
Acknowledgments
Part I: Introducing the Concept of Web scripting and the JavaScript Language
Part II: Learning JavaScript Basics
Part III: Learning More About the DOM
Part IV: Working with Advanced JavaScript Features
Part V: Building Multimedia Applications with JavaScript
Hour 19. Using Graphics and Animation
Using Dynamic Images
Creating Rollovers
A Simple JavaScript Slideshow
Summary
Q&A
Quiz Questions
Quiz Answers
Exercises
Hour 20. Working with Sound and Plug-Ins
Part VI: Creating Complex Scripts
Part VII: Appendixes
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele