Javascript Confirm Prompt
2002-09-14 18:43:26
Category: javascript:general
Description: Example on how to use the prompt and confirm function's
Author: mind
Viewed: 35367
Rating: (79 votes)


<!-- confirm_prompt.js - mind (mind@metalshell.com) -->
 
<!-- this example shows how to use the prompt and confirm -->
<!-- function's to ask questions and confirm the results -->
 
<!-- http://www.metalshell.com -->
 
<!-- start our javascript -->
<script language=JavaScript>
        // create our function
        function QuestIon()
        {
                // prompt("message", "message2");
                // message will be the header of the popup box
                // message2 will be written to the text area
 
                // create a variable storing the results of prompt
                var inPut = prompt("Enter your name", "");
 
                // check to see if anything was entered
                // null == pressing cancel
                // ""   == entered nothing and pressing ok
                if (inPut == null || inPut == "")
                {
                        // confirm("message..") pops up a window with
                        // 'message..' with two buttons (ok/cancel)
 
                        // print error message with confirmation buttons
                        // returns 1 on success 0 on failure..
                        if (confirm("Nothing Entered, Try again?"))
                        {
                                // create a variable (inPut) storing the
                                // results of prompt();
                                var inPut = prompt("Enter your name", "");
 
                                // check to see if anything was enetered
                                if (inPut == null || inPut == "")
                                {
                                        // print failure
                                        alert("Nothing was entered.");
 
                                        // exit our javascript
                                        return;
                                }
 
                                // print success/results
                                document.write("Hello, " + inPut);
                        }
                }
 
                // inPut was neither null nor ""
                else
                {
                        // print success/results
                        document.write("Hello, " + inPut);
                }
        }
 
        // end of our javascript
</script>
 
<!-- place this code anywhere on your html document (example) -->
 
<!-- align our link below -->
<div align=center>
        <!-- make a link that points to our js function QuestIon() -->
        <a href="javascript: QuestIon();">Click Here!</a>
</div>