04.11.2015 Views

javascript

Create successful ePaper yourself

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

Chapter 8: The Browser Object Model<br />

If the OK button is clicked, prompt() returns the value in the text box; if Cancel is clicked or the dialog<br />

is otherwise closed without clicking OK, the function returns null . Here ’ s an example:<br />

var result = prompt(“What is your name? “, “”);<br />

if (result !== null) {<br />

alert(“Welcome, “ + result);<br />

}<br />

These system dialogs can be helpful for displaying information to the user and asking for confirmation<br />

of decisions. Since they require no HTML, CSS, or JavaScript to be loaded, they are fast and easy ways to<br />

enhance a web application.<br />

Google Chrome introduced a new feature regarding these system dialogs. If the actively running script<br />

produces two or more system dialogs during its execution, each subsequent dialog after the first displays<br />

a check box that allows the user to disable any further dialogs until the page reloads (see Figure 8 - 6 ).<br />

Figure 8-6<br />

When the check box is checked and the dialog box is dismissed, all further system dialogs (alerts,<br />

confirms, and prompts) are blocked until the page is reloaded. Chrome 0.2 gives the developer no<br />

indication as to whether the dialog was displayed. The dialog counter resets whenever the browser is<br />

idle, so if two separate user actions produce an alert, the check box will not be displayed in either; if a<br />

single user action produces two alerts in a row, the second will contain the check box. It ’ s expected that<br />

this feature will be refined to give developers more information about the success of the dialogs by the<br />

time the browser reaches version 1.0.<br />

Two other types of dialogs can be displayed from JavaScript: find and print. Both of these dialogs are<br />

displayed asynchronously, returning control to the script immediately. The dialogs are the same as the<br />

ones the browser employs when the user selects either Find or Print from the browser ’ s menu. These are<br />

displayed using the find() and print() methods on the window object as follows:<br />

//display print dialog<br />

window.print();<br />

//display find dialog<br />

window.find();<br />

These two methods give no indication as to whether the user has done anything with the dialog, so it is<br />

difficult to make good use of them. Further, since they are asynchronous, they don ’ t contribute to<br />

Chrome ’ s dialog counter and won ’ t be affected by the user opting to disallow further dialogs.<br />

215

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

Saved successfully!

Ooh no, something went wrong!