15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

326<br />

IMG.src<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

The page (see Figure 6-1) includes two IMG elements: one that displays noncached<br />

images and one that displays cached images. Under each image is a SELECT<br />

element that you can use to select one of four possible image files for each element.<br />

The onChange event handler for each SELECT list invokes a different function to<br />

change the noncached (loadIndividual()) or cached (loadCached()) images.<br />

Both of these functions take as their single parameter a reference to the form that<br />

contains the SELECT elements.<br />

To cycle through images at five-second intervals, the checkTimer() function<br />

looks to see if the timer check box is checked. If so, the selectedIndex property of<br />

the cached image SELECT control is copied and incremented (or reset to zero if the<br />

index is at the maximum value). The SELECT element is adjusted, so you can now<br />

invoke the loadCached() function to read the currently selected item and set the<br />

image accordingly.<br />

For some extra style points, the tag includes an onUnload event handler<br />

that invokes the resetSelects() function. This general-purpose function loops<br />

through all forms on the page and all elements within each form. For every SELECT<br />

element, the selectedIndex property is reset to zero. Thus, if a user reloads the<br />

page, or returns to the page via the Back button, the images start in their original<br />

sequence. An onLoad event handler makes sure that the images are in sync with the<br />

SELECT choices and the checkTimer() function is invoked with a five-second delay.<br />

Unless the timer check box is checked, however, the cached images don’t cycle.<br />

Listing 22-4: A Scripted Image Object and Rotating Images<br />

<br />

<br />

Image Object<br />

<br />

// global declaration for ‘desk’ images array<br />

var imageDB<br />

// pre-cache the ‘desk’ images<br />

if (document.images) {<br />

// list array index names for convenience<br />

var deskImages = new Array(“desk1”, “desk2”, “desk3”, “desk4”)<br />

// build image array and pre-cache them<br />

imageDB = new Array(4)<br />

for (var i = 0; i < imageDB.length ; i++) {<br />

imageDB[deskImages[i]] = new Image(120,90)<br />

imageDB[deskImages[i]].src = deskImages[i] + “.gif”<br />

}<br />

}<br />

// change image of ‘individual’ image<br />

function loadIndividual(form) {<br />

if (document.images) {<br />

var gifName = form.individual.options[form.individual.selectedIndex].value<br />

document.thumbnail1.src = gifName + “.gif”<br />

}<br />

}<br />

// change image of ‘cached’ image<br />

function loadCached(form) {<br />

if (document.images) {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!