Well, let's say you wanted to get somebody's name before they saw the page, and then write their name on your page right before their very eyes.......Well, you can do this using a javascript prompt. Here's the command:
prompt('Your Question', ' ');
This will bring up a window asking the question of your choosing, with a space for the viewer to answer. The second set of quotes allows you to enter a default answer. If you leave it blank, the viewer will just see an empty box ready to be typed in. This is usually done before the page loads, so that you can write the answer they give into your page. To view an example, click the link below. You will get a prompt for your name, and your name will be written on the page!
Now, for the script that made this work.
Note how the prompt and if/else statements are in the HEAD section (and is automatically shown when the page loads), while the actual writing of the name occurs in the BODY section
<SCRIPT language="JavaScript">
<!--hide
var yourname= prompt('Please enter your name, so you can get a special greeting', ' ');
if ( (yourname==' ') || (yourname==null) )
{
yourname="Dude";
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT language="JavaScript">
<!--hide
document.write("<CENTER><H1>Hello, " + yourname + " ! Welcome to My Page! <\/H1><\/CENTER>");
//-->
</SCRIPT>
</BODY>
The first thing that happens is that the variable yourname is assigned the value it receives from the user from the prompt.
here's the script:
<SCRIPT LANGUAGE = JavaScript>
function Prompter()
{
var text = new String()
text = window.prompt("Do you program in JavaScript?", "Yes, I do")
if (text == "")
document.form1.Textbox.value = "You didn't enter anything."
else
document.form1.Textbox.value = "You entered: " + text
}
</SCRIPT>
and here's what I slipped into the button's "onclick" event:
<INPUT TYPE = BUTTON Value = "Click Me" onClick = "Prompter()">