A method of the 'window' object [like 'alert()'] is 'prompt().' prompt() asks the viewer for input in response to a question. prompt() can store the viewer's response in a variable. Let's use prompt() to ask the viewer for her name, and then print her name to the screen with a welcome message. Here's the code:
userName = prompt("What is your first name?"," ")
var res = "<p>Nice to meet you, " + userName + "."
Inside the first set of double-quotes in prompt() is the question. Inside the second set of double-quotes is a default answer that shows up in the answer space. If you want the answer space to be blank, leave the second set of double-quotes empty.
The viewer's response gets stored in the variable 'userName.' res.fontsize("4") determines the font size of the output text. (A fontsize should be between 1 and 7.)
Try these links on simple math and variables:
|Prompts and Variables||www.htmlgoodies.com/primers/jsp/article.php/3478471|
or check out one of our reference books, or look for other help on the Web.
Work on either 1) your own web page, or 2) the eEverything.com site.
If you work on eEverything.com, use 'prompt()'s, variables and some math to create a simple sales-tax calculator. Using 2 'prompt()'s, ask the user for the name and price of an item. Using a third 'prompt()', ask the user what the sales tax rate is. (Assume that the sales tax rate is 6.625% and have 0.06625 show up as the default in the prompt). An example:
var taxRate = prompt("What is the tax rate, as a decimal?", "0.06625")
Then use 'document.write()' to report the results. Output will be something like: "Sales tax on the $1000 leather sofa is $66.25. The total cost is $1066.25."
If you work on your own page, implement something similar.