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.

178<br />

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

Caution<br />

One warning about setTimeout() functions that dive into themselves as frequently<br />

as this one does: Each call eats up a bit more memory for the browser<br />

application in Navigator 2. If you let this clock run for a while, some browsers may<br />

encounter memory difficulties, depending on which operating system they’re<br />

using. But considering the amount of time the typical user spends on Web pages<br />

(even if only 10 or 15 minutes), the function shouldn’t present a problem. And any<br />

reloading invoked by the user (such as by resizing the window in Navigator 2)<br />

frees up memory once again.<br />

showModalDialog(“URL”[, arguments]<br />

[, features])<br />

showModelessDialog(“URL”[, arguments]<br />

[, features])<br />

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

Compatibility (✓) ✓ ✓<br />

Example<br />

To demonstrate the two styles of dialog boxes, I have implemented the same<br />

functionality (setting some session visual preferences) for both modal and modeless<br />

dialog boxes. This tactic shows you how to pass data back and forth between<br />

the main page and both styles of dialog box windows.<br />

The first example demonstrates how to use a modal dialog box. In the process,<br />

data is passed into the dialog box window and values are returned. Listing 16-39 is<br />

the HTML and scripting for the main page. A button’s onClick event handler invokes<br />

a function that opens the modal dialog box. The dialog box’s document (Listing<br />

16-40) contains several form elements for entering a user name and selecting a few<br />

color styles for the main page. Data from the dialog is fashioned into an array to be<br />

sent back to the main window. That array is initially assigned to a local variable,<br />

prefs, as the dialog box closes. If the user cancels the dialog box, the returned value<br />

is an empty string, so nothing more in getPrefsData() executes. But when the user<br />

clicks OK, the array comes back. Each of the array items is read and assigned to its<br />

respective form value or style property. These values are also preserved in the global<br />

currPrefs array. This allows the settings to be sent to the modal dialog box (as the<br />

second parameter to showModalDialog()) the next time the dialog box is opened.<br />

Listing 16-39: Main Page for showModalDialog()<br />

<br />

<br />

window.setModalDialog() Method<br />

<br />

var currPrefs = new Array()<br />

windowObject.showModalDialog()

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

Saved successfully!

Ooh no, something went wrong!