Exforsys

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction

Ads


Home arrow Technical Training arrow JavaScript Tutorial

JavaScript Confirm Box

Author : Exforsys Inc.     Published on: 25th Apr 2007    |   Last Updated on: 7th Dec 2010

JavaScript Confirm Box

In the previous section of this tutorial JavaScript Alert Box, we discussed about alert boxes. In this section, you will learn about JavaScript confirm box and prompt box.

Ads

Confirm Box:

The confirm box is a box that pops up with both an OK and a Cancel button. The confirm box is used to verify acceptance from the user. If the user accepts, then the user presses the OK button and the confirm box returns with a true value. If the user rejects with the Cancel button, then the confirm box returns false value.

General syntax for a confirm box is

confirm (“textmessage”)

A simple confirm box example:


<
html>
   <body>
    
<script type="text/javascript">
        if (confirm("Wish to accept or Cancel"))
        {         

           alert ("Agreed and hence True value returned")

        }

        else

        {         

           alert ("Not Agree and hence False value returned")

        }

      </script>
   </body>
</html>

In the above example, the conditional statement if…else is used and the alert box and the confirm box are written together.

Output of the above script as produced in a HTML page is shown below:

The confirm box pops up with the message:

Wish to accept or Cancel

Showing two buttons (OK and Cancel) that the user can choose from. If the user presses OK in the confirm box then the value returned would be true, executing the if block of statements. This results in the alert box popping up with the message.

Agreed and hence true value returned.

If the user presses the Cancel button in the confirm box then the value returned would be false, executing the else block of statements. This results in the alert box popping up with the message

Not Agreed and hence false value returned

Prompt Box:

The prompt box is used when a user’s input value is desired before entering a page. The prompt box pops up with two buttons (OK and Cancel). If the user presses OK then the prompt box returns with the value entered by user but if the user presses the Cancel button then the prompt box returns with a null value.

General syntax of prompt box is as follows:

prompt(“textmessage”,”value_default”)

A simple prompt box example:


<
html>
   <body>
    
<script type="text/javascript">
        name = prompt ("Input the Training Company Name","Exforsys")
    </script>
   </body>
</html>

 

Output of the above script as produced in a HTML page is shown below:

Ads

In the above example, the default value is stored with value Exforsys and the prompt box pops up with the message Input the Training Company Name with a default value as Exforsys and two buttons (OK and Cancel). When the user presses the OK button, the value Exforsys is returned by prompt box, which is stored in the variable name. If the user presses Cancel, then null value is retuned by the prompt box.



 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments