Using the prompt() Method to Assign a Value to a Variable

So far, you've seen examples of how to assign values to variables directly in the JavaScript. Sometimes, though, you can't do this. For example, if you ask the user a question, such as her name, you have no possible way to know what the answer might be (unless perhaps you have only one person visiting your site!).

Fortunately, assigning values to variables in mid-script is easy. The following example shows you how to use the prompt() method to accomplish this.

You once again begin with your blank HTML template page and into the <script> block add the prompt() method:

 <html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!-- Cloaking device on! prompt(); // Cloaking ...

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.