15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

330<br />

IMG.onLoad<br />

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

onLoad<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓ ✓<br />

Example<br />

Quit and restart your browser to get the most from Listing 22-5. As the document<br />

first loads, the LOWSRC image file (the picture of pencil erasers) loads ahead of the<br />

computer keyboard image. When the erasers are loaded, the onLoad event handler<br />

writes “done” to the text field even though the main image is not loaded yet. You<br />

can experiment further by loading the arch image. This image takes longer to load,<br />

so the LOWSRC image (set on the fly, in this case) loads way ahead of it.<br />

Listing 22-5: The Image onLoad Event Handler<br />

<br />

<br />

<br />

function loadIt(theImage,form) {<br />

if (document.images) {<br />

form.result.value = “”<br />

document.images[0].lowsrc = “desk1.gif”<br />

document.images[0].src = theImage<br />

}<br />

}<br />

function checkLoad(form) {<br />

if (document.images) {<br />

form.result.value = document.images[0].complete<br />

}<br />

}<br />

function signal() {<br />

if(confirm(“You have stopped the image from loading. Do you want to try<br />

again?”)) {<br />

location.reload()<br />

}<br />

}<br />

<br />

<br />

<br />

<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!