Lesson 8: The Prompt Function



In this lesson we will demonstate how the prompt function is used to display prompt boxes on your web page. The prompt function is called the same as the alert and confirm functions. All you do is write: promp() and put a quoted text string in the parenteses for what you want in the prompt box. The prompt box allows you to display a statement in a text box and it also allows the user to enter a response. That response can then be stored in a javascript varible for use in your script. lets look at some examples:


Structure:



Prompt("what you want it to say here")



Working Example:


<html>

<body>

<script type="text/javascript">

var name1 = prompt("Please enter your name:")

alert("your name is: "+name1)

</script>

</body>

</html>


Go Ahead and try it! Notice here that we have used the minimal tags nessasary to test the script in your browser. This format should however never be used in a real html document. For your real script make sure you use all the proper tags as described earlier. The above code will work if you cut and paste it to your notepad, save it as an HTML document and open it in your browser.


Now let's break down the code: First lines 1 - 3 you should know opens our HTML document and lets the browser know that it has encountered a script. Now on line 4 we declare the variable: name1 and assign it the value of what is entered into the prompt box that displays the text: please enter your name:. In line 5 we use the alert function to display an alert box that says: "your name is", then it concatenates the variable name1 to the text and the result is the computer telling the user what their name is!

                    

Copyright © 2012 VisualBuilder. All rights reserved