Getting input from the user
We can use JavaScript's alert command to display information
to the user but we also need to be able to read the user's input. We can use the
prompt command to do this.
Type this html page into NotePad++:
<html>
<!-- Getting input from the user -->
<head>
<title>My second program</title>
</head>
<body>
<script>
userName = prompt("What is your name", "")
alert("Hello " + userName + ".")
</script>
This text will appear after you close the alert.
</body>
</html>
Save the file and open it in FireFox.
You should see a box on screen asking for your name.
Enter your name and you should see a welcome message.
This program introduces the concepts of
variables
,
strings
and
parameters
in JavaScript.
|