JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

14.5 Dialog Boxes

14.5 Dialog Boxes

The Window object provides three methods for displaying simple dialog boxes to the user. alert()displays a message to the user and waits for the user to dismiss the dialog. confirm() displays a message, waits for the user to click an OK or Cancel button and returns a boolean value. And prompt() displays a message, waits for the user to enter a string, and returns that string. The following code uses all three methods:

do { var name = prompt("What is your name?"); // Get a string var correct = confirm("You entered '" + name + "'.\n" + // Get a boolean

"Click Okay to proceed or Cancel to re-enter."); } while(!correct) alert("Hello, " + name); // Display a plain message

Although the alert(), confirm(), and prompt() methods are very easy to use, good design dictates that you use them sparingly, if at all. Dialog boxes like these are not a common feature on the Web, and most users will find the dialog boxes produced by these methods disruptive to their browsing experience. The only common use for these methods today is debugging: JavaScript programmers sometimes insert alert() methods in code that is not working in an attempt to diagnose the problem.

Note that the messages displayed by alert(), confirm(), and prompt() are plain text, not HTML-formatted text. You can format these dialog boxes only with spaces, new-lines, and punctuation characters.

The confirm()and prompt()methods block—that is, these methods do not return until the user dismisses the dialog boxes they display. This means that when you pop up one of these boxes, your code stops running, and the currently loading document, if any, stops loading until the user responds with the requested input. In most browsers, the alert() method also blocks and waits for the user to dismiss the dialog box, but this is not required. For complete details on these methods, see Window.alert, Window.con firm, and Window.prompt in Part IV .

In addition to the Window methods alert(), confirm(), and prompt(), a more complicated method, showModalDialog(), displays a modal dialog box containing HTML-formatted content and allows arguments to be passed to, and a value returned from, the dialog. showModalDialog() displays a modal dialog in a browser window of its own. The first argument is the URL that specifies the HTML content of the dialog box. The second argument is an arbitrary value (arrays and objects are allowed) that will be made available to scripts in the dialog as the value of the window.dialogArguments property. The third argument is a nonstandard list of semicolon-separated name=value pairs that, if supported, may configure the size or other attributes of the dialog. Use “dialogwidth” and “dialogheight” to set the size of the dialog window, and use “resizable=yes” to allow the user to resize the window.

The window displayed by this method is modal, and the call to showModalDialog()does not return until the window is closed. When the window closes, the value of the window.returnValue property becomes the return value of the method call. The HTML content of the dialog must typically include an OK button that sets returnValue, if desired, and calls window.close() (see § ).

Example 14-4 is an HTML file suitable for use with showModalDialog(). The comment at the top of the code includes a sample invocation of showModalDialog(), and Fig ure 14-1 shows the dialog created by the sample call. Note that most of the text that appears in the dialog comes from the second argument to showModalDialog(), rather than being hard-coded in the HTML.

14.5 Dialog Boxes | 349

Example 14-4. An HTML file for use with showModalDialog()


友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1