www.crosswinds.net/~fishcaro/day_J7_getting_input.htm
INDEX CARD #J7:
the "prompt" command (J7a)Use the following command to bring up a dialog box with a specified message. The default value puts a starting value in the dialog box. prompt(message,default); For example, the code:
prompt("Please type your name:","First Middle Last")
will cause something like this to appear: |
Getting an Empty Dialog Box (J7b)"" for the default value to display an
empty input box, like this:
prompt("Please type your name:","")
It will cause something like this to appear: NOTE: The text displayed in the "prompt" dialog box can only be plain text, not HTML-formatted text. Also, there's no way to get rid of the word "Script Prompt" (different browsers say slightly different things). It is there to prevent people from writing code that tries to imitate system dialogs and trick users into entering their passwords and such. |
Return Value from the prompt command (J7c)prompt command is:
<SCRIPT LANGUAGE="JavaScript"> If you want the name formatted in a fancy way, just insert some style info, like this:
document.write("Well, <font style='color: green; font-family: fantasy; text-transform: uppercase'>" + ret + "</font>, it\'s nice to have you here!");
|
Changing a String to a Number (J7d)
num = prompt("Please input a number:","number"); Here's what's happening: JavaScript is smart enough to know that if you're multiplying by one, then you must REALLY want a number, so it changes its type for you. Be sure to try out the code above! Then, REMOVE THE CODE "num = num*1" and see what happens; explain why! |
Printable version of Index Card J7a
Printable version of Index Card J7b
Printable version of Index Card J7c
Printable version of Index Card J7d
WORKSHEET #J7:
prompt
command by writing the
following, and studying the results:
ret = prompt("a test:","name");
document.write(ret,"<BR>");