JavaScript Prompt (JavaScript Tutorial part 19)

১১/২১/২০১১ ১২:১১:০০ AM | , | 0 comments »

The JavaScript prompt is a relic from the 1990's that you seldom see being used in modern day websites. The point of the JavaScript prompt is to gather information from the user so that the information can be used throughout the site to give the visitor a personalized feel.

Back in the day, you'd often see prompts on personal webpages asking for your name. After you typed in the information, you would be greeted with a page that had a welcome message, such as, "Welcome to My Personal WebPage John Schmieger!" (If your name just so happened to be John Schmieger).
The JavaScript prompt is not very useful and many find it slightly annoying, but hey, this tutorial is here to educate you, so let's learn how to make that prompt!

Simple JavaScript Prompt

You can use a prompt for a wide variety of useless tasks, but below we use it for an exceptionally silly task. Our prompt is used to gather the user's name to be displayed in our alert dialogue box.

HTML & JavaScript Code:

<head>
<script type="text/javascript">
<!--
function prompter() {
var reply = prompt("Hey there, good looking stranger!  What's your name?", "")
alert ( "Nice to see you around these parts " + reply + "!")
}
//-->
</script>
</head>
<body>
<input type="button" onclick="prompter()" value="Say my name!">
</body>

Display:

Recap on JavaScript Prompt

It sure is a quick way to gather some information, but it is not as reliable an information gatherer as other options available to you. If you want to find out someone's name and information, the best way to request this information would be through the use of HTML Forms. And if you want to use the information you collected in your website, you might use some PHP to get that job done in a more sophisticated manner.