Asking for User Data


var name =   window.prompt("Enter your name!", "<Your name>"); 

The window.prompt() method allows users to enter text into a single-line text field (see Figure 2.2). This information is the return value of the method call and can be further used in the script.

Figure 2.2. The input box generated by window.prompt().


Prompting for User Data (prompt.html)

<script language="JavaScript"   type="text/javascript"> var name =   window.prompt("Enter your name!", "<Your name>"); if (name != null) {   window.alert("Hello, " + name + "!"); } </script> 

Note

Do note that if the Cancel button is clicked or the Esc key is pressed, window.prompt() returns null. The preceding code checks that; if the OK button is pressed, the data entered is shown.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

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