4.1 Interacting with the UserPrograms like to talk, ask questions, get answers, and respond. In the previous chapter, we saw how the write() and writeln () methods are used to send output to the browser. A method is how you do something, and JavaScript methods are the action words, the "doers" of the JavaScript language. They make things happen. JavaScript uses dialog boxes to interact with the user. The dialog boxes are created with three methods:
4.1.1 The alert() MethodWe saw in the last chapter that the write() and writeln() were JavaScript methods used to send output to the Web page. Another way to send output to the browser is with the alert() method. The alert() method creates a little independent box ”called a dialog box ”which contains a small triangle with an exclamation point. A user-customized message is placed after the triangle, and beneath it, an OK button. When the dialog box pops up, all execution is stopped until the user presses the OK button in the pop-up box. The exact appearance of this dialog box may differ slightly on different browsers, but its functionality is the same. The message for the alert dialog box is a string of text (or a valid expression) enclosed in double quotes, and sent as a single argument to the alert() method. HTML tags are not rendered within the message string but you can use the escape sequences, \n and \t . FORMAT alert("String of plain text"); alert(expression); Example: alert("Phone number is incorrect"); alert(a + b); Example 4.1<html> <head><title>Dialog Box</title></head> <body bgcolor="yellow" text="blue"> <b>Testing the alert method</b><br> 1 <script language="JavaScript"> 2 document.write("<font size='+2'>"); document.write("It's a bird, "); 3 document.write("It's a plane, <br>"); 4 alert("It's Superman!"); 5 </script> </body> </html> EXPLANATION
Example 4.2<html> <head> <title>Using JavaScript alert box</title> 1 <script language="JavaScript"> 2 alert("Welcome to\nJavaScript Programming!"); 3 var message1="Match your Quotes and "; 4 var message2="Beware of Little Bugs "; 5 alert(message1 + message2); </script </head> </html> EXPLANATION
4.1.2 The Prompt BoxSince JavaScript does not provide a simple method for accepting user input [as it does for sending output with document.write() ], the prompt dialog box and HTML forms are used (forms are discussed in Chapter 11, "The Document Objects: Forms, Images, and Links"). The prompt dialog box pops up with a simple textfield box. After the user enters text into the prompt dialog box, its value is returned. The prompt dialog box takes two arguments: a string of text that is normally displayed as a question to the user, prompting him to do something, and another string of text which is the initial default setting for the box. If this argument is an empty string, nothing is displayed in the box. The prompt method always returns a value. If the user presses the OK button, all the text in the box is returned; otherwise null is returned. FORMAT prompt(message); prompt(message, defaultText); Example: prompt("What is your name? ", ""); prompt("Where is your name? ", name); Example 4.3<html> <head> <title>Using the JavaScript prompt box</title> </head> <body> <script language = "JavaScript"> 1 var name=prompt("What is your name?", ""); 2 document.write("<br>Welcome to my world! " + name + ".</font><br>"); 3 var age=prompt("Tell me your age.", "Age"); 4 if ( age == null){ // If user presses the cancel button 5 alert("Not sharing your age with me"); 6 } else{ 7 alert(age + " is young"); } 8 alert(prompt("Where do you live? ", "")); </script> </body> </html> EXPLANATION
Figure 4.4. Prompt with a default argument.
Figure 4.5. Prompt without a default.
Figure 4.6. User types in the prompt box.
4.1.3 The Confirm BoxThe confirm dialog box is used to confirm a user's answer to a question. A question mark will appear in the box with an OK button and a Cancel button. If the user presses the OK button, true is returned; if he presses the Cancel button, false is returned. This method takes only one argument, the question you will ask the user. Example 4.4<html> <head> <title>Using the JavaScript confirm box</title> </head> <body> <script language = "JavaScript"> document.clear // Clears the page 1 if(confirm("Are you really OK?") == true){ 2 alert("Then we can proceed!"); } else{ 3 alert("We'll try when you feel better? "); } </script> </body> </html> EXPLANATION
|