|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Displaying Dialog BoxesThe window object includes three methods that are useful for displaying messages and interacting with the user. You've already used these in some of your scripts. Here's a summary:
To use the confirm() and prompt() methods, use a variable to receive the user's response. For example, this statement displays a prompt and stores the text the user enters in the text variable: text = window.prompt("Enter some text","Default value");
Did you Know? You can usually omit the window object when referring to these methods because it is the default context of a script (for example, alert("text")). Creating a Script to Display Dialog BoxesAs a further illustration of these types of dialog boxes, Listing 10.4 shows an HTML document that uses buttons and event handlers to enable you to test dialog boxes. Listing 10.4. An HTML Document That Uses JavaScript to Display Alerts, Confirmations, and Prompts
This document displays three buttons, and each one uses an event handler to display one of the dialog boxes. Figure 10.5 shows the script in Listing 10.4 in action. The prompt dialog box is currently displayed and shows the default value. Figure 10.5. The dialog box example's output, including a prompt dialog box.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |