Client-Side Scripting with JavaScript


Part 6 - Getting Input from Users


Data may be received from the user with the window.prompt command:

     variable = window.prompt(display text, initial value)
	 
This script ran when the current page loaded:

<SCRIPT LANGUAGE="JavaScript"> user = window.prompt("Please enter your name: ", ""); document.write("Welcome, <B>", user, "</B> to this part of the workshop."); </SCRIPT>
Another example of entering data from a prompt:

<SCRIPT LANGUAGE="JavaScript"> bg = new Array("#FFFFFF", "#00FFFF", "#0000FF", "#00FF00"); bgcolor = 0; bgcolor = window.prompt("Background color (0-White 1-Aqua 2-Blue 3-Lime): ", 0); document.bgColor = bg[bgcolor]; </SCRIPT> You can also display a message to the user with the window.alert command:

<SCRIPT LANGUAGE="JavaScript"> window.alert("Your name is: " + user); </SCRIPT> Click here to try it out.

Pros and Cons

Better alternative: HTML form tags.

Next Next: HTML Forms