Using the Confirm Box in JavaScript

Using the Confirm Box in JavaScript

The confirm box in JavaScript is an advanced form of an alert box. It is used to display messages as well as return a value (true or false). The confirm box displays a dialog box with two buttons, OK and Cancel. When the user clicks on the OK button, it returns true, and when the user clicks on the Cancel button, it returns false.

The confirm box enables you to interrupt the JavaScript processing to ask the user a question and then continue processing based on which button is clicked.

Demo

  1. Create a document named confirmBox.html.

    Confirm Box in JavaScript

  2. Write the following code,
    <!DOCTYPE HTML>
    <HTML>
    <HEAD>
    <TITLE>USING THE CONFIRM BOX</TITLE>
    </HEAD>
    <BODY>
    <SCRIPT type="text/javascript">
    var query=confirm("Are you sure you want to send this file to Recycle Bin?"); if(query)
    {
    }
    else
    (
    document.write("Welcome to JavaScript. You have moved this file to Recycle Bin");
    document.write("Welcome to JavaScript. You have not moved this file. ");
    </SCRIPT>
    </BODY>
    </HTML>
  3. Execute the script by Opening the file in the Web Browser. If you are using Internet Explorer, click on “Allow Blocked Content” to allow the script to execute, and if you are using Mozilla Firefox, then click on allow “ActiveX Controls”.

    Confirm Box in JavaScript

    When you select OK,

    Confirm Box in JavaScript

    When you select Cancel,

    Confirm Box in JavaScript

Thanks for reading this blog.