Software Sue

Link to Simply Sue
Link to Sustainability Sue
location: software/scripting/javascript/prompt Skip navigation : Home  

JavaScript Prompts


When you cam to this page, you should have been prompted to input a number. The code in the looks like this:

var prompt_reply = prompt("Think of a number and type it in this box","This is an example of a prompt");
var your_guess = " The number that you guessed was ";
var your_result = your_guess + prompt_reply + "!";

Take a look at the source for this page. The first line assigns a variable (prompt_reply to take the result of the prompt. "Think of a number and type it in this box" instructs the user what to reply to the prompt, and "This is an example of a prompt" is the default value for the variable, which is displayed but can be typed over. If you don't want a default, use an empty string (""). The second line assigns the string " The number that you guessed was " to the variable your_guess. The third line concatenates the two plus an explanation mark to form a sentence, which is stored in your_result.

In the body of the page, the following code displays your_result, which contains the sentence "The number that you guessed was: 12!", where 12 is the number I typed in at the prompt.

document.writeln(your_result);