Did you notice that little user input I slipped in in the last lesson? It was that radius box in the circle area program. A more overt method of asking something of your viewer makes use of the PROMPT command:
<HTML>
<HEAD>
<SCRIPT TYPE="text/javascript">
function getName()
{
var name=prompt("What is your name?","John Doe");
alert(name + " is a complete goof!")
}
</SCRIPT>
</HEAD>
<BODY>
<INPUT TYPE="button" ONCLICK="getName()" VALUE="Name">
</BODY>
</HTML>
Notice I used "What is your name?" instead of "What's your name?"? That is because you can't use an apostrophe without an escape code (\) to indicate it is part of the text rather than a quote. You can use single and double quotes interchangeably and when nesting quotes. So the following would have been the way to use "What's":
var name=prompt("What\'s your name?","John Doe");
More on Escape CodesA little formal instruction is called for at this point. The most general description of a program is software that:
And though this seems a little outdated, it still allows us to categorize commands. We have looked at OUTPUT in the form of the write and alert commands as well as setting the value property of a text box. We saw INPUT using the value property of a text box as well as the prompt command. The one PROCESS we looked at was multiplication to give us the area of a circle from the radius. We will use these classifications more as we go along.
Another thing to point out at this stage is the use of variables. We used our first variable "name" to hold the name the user typed in the prompt box. We declared it (and assigned it a value at the same time) using the keyword var. It is not required that you use var if you declare and assign a value to a variable (initialize) at the same time, but if you want to declare the varible to use later you must use the var keyword.
var userName;
myName="Mrs. B";
var hisName="John Doe";
Notice the semicolons? You don't need them but because C++ requires them and because Javascript has such a similarity to C++, I use them.
Javascript is a loosely typed language. In fact, there is no facility to set types for variables. name could be assigned "John Doe" then you could set it equal to 3.14, then give it a value of TRUE and it wouldn't produce an error. But don't.
The usual practice for naming variables is to use lower case for a single word or the first word in a multiple word name, then capitalize the first letter of all words after the first. So "name" was lower case but it would be "userName" if we wanted to use multiple words. Variables can be helpful when trying to read a program so use good names.