Creating a Prompt


In a moment, I’ll show you how to do something really fun with a for loop— namely, reverse the text entered by the user. But before we get there, I need to show you a preliminary example.

In Chapter 3, “ Using Conditional Statements,” I showed you how to display an alert box, which provides a message to the user.

A prompt box provides a similar sort of facility except that it also lets the user enter input.

Note

By the way, you should expect the appearance and user prompt to vary in different browsers. I show Internet Explorer in these figures, but things won’t quite look the same in Mozilla or Netscape.

Just as the alert function displays an alert box, there’s a JavaScript function, prompt, that displays a prompt box and requests input from the user. The general form is as follows:

 ReturnValue = prompt (message, default text); 

For example, this assignment:

 retVal = prompt("Enter your text, please!",     "How doth the little crocodile?"); 

produces the prompt box shown in Figure 4-1.

click to expand
Figure 4-1: The prompt function displays a box that accepts user input.

As you can see, by default text in the general form provided previously, I mean the text initially seen by the user in the prompt box. If the user doesn’t change the default text and clicks OK, the default text is the value sent back to the JavaScript variable (retVal in the example). However, if the user enters their own value in the prompt box—for example, I’ll do it my way, thank you very much! —then that’s the value passed back to the JavaScript variable.

Now that you know how prompt boxes work, let’s go ahead and put them to use in conjunction with a loop.

start sidebar
I’m Blocked, How About You?

A prompt box is modal, meaning that no further program statements will be executed until the user chooses OK or Cancel. Another way of putting this is that all other code statements in the program are blocked until the user takes action.

end sidebar




Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net