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.

180<br />

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

Buttons at the bottom of the page are explicitly positioned to be at the lowerright<br />

corner of the window. Each button invokes a function to do what is needed<br />

to close the dialog box. In the case of the OK button, the handleOK() function<br />

sets the window.returnValue property to the data that come back from the<br />

getFormData() function. This latter function reads the form element values and<br />

packages them in an array using the form elements’ names as array indices. This<br />

helps keep everything straight back in the main window’s script, which uses the<br />

index names, and is therefore not dependent upon the precise sequence of the form<br />

elements in the dialog box window.<br />

Listing 16-40: Document for the Modal Dialog<br />

<br />

<br />

User Preferences<br />

<br />

// Close the dialog<br />

function closeme() {<br />

window.close()<br />

}<br />

// Handle click of OK button<br />

function handleOK() {<br />

window.returnValue = getFormData()<br />

closeme()<br />

}<br />

// Handle click of Cancel button<br />

function handleCancel() {<br />

window.returnValue = “”<br />

closeme()<br />

}<br />

// Generic function converts form element name-value pairs<br />

// into an array<br />

function getFormData() {<br />

var form = document.prefs<br />

var returnedData = new Array()<br />

// Harvest values for each type of form element<br />

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

if (form.elements[i].type == “text”) {<br />

returnedData[form.elements[i].name] = form.elements[i].value<br />

} else if (form.elements[i].type.indexOf(“select”) != -1) {<br />

returnedData[form.elements[i].name] =<br />

form.elements[i].options[form.elements[i].selectedIndex].value<br />

} else if (form.elements[i].type == “radio”) {<br />

returnedData[form.elements[i].name] = form.elements[i].value<br />

} else if (form.elements[i].type == “checkbox”) {<br />

returnedData[form.elements[i].name] = form.elements[i].value<br />

} else continue<br />

}<br />

return returnedData<br />

}<br />

windowObject.showModalDialog()

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

Saved successfully!

Ooh no, something went wrong!