LearnFast.Ninja

JavaScript confirm dialog box example

As JavaScript or Web Developer you need to be familiar with all the JavaScript popup alert types. One of them is the confirm dialog box. You will use it when you want to confirm or verify something from the user. For example if the user want to leave a page and all the data are going to be lost you can ask him "Are you sure you want to exit this page?" then if the user click OK you will get to the var a true value. If the user clicked CANCEL you will get false value. Then you can build if statement and write how to deal with the user response.

How it looks like:


What part of the confirm dialog you can edit? 
Only the message part, because the confirm function get only parameter which is string value.



Main idea:
var Answer = confirm("This is my message.");

Working example code:

<!DOCTYPE html>

<html>

<head>

    <title>HTML5 Page</title>

    <script type="text/javascript">

        function Main() {

            var Answer = confirm("This is my message.");

            if (Answer == true) {

                alert("OK is clicked.");

            }

            else {

                alert("Cancel is clicked.");

            }

        }

    </script>

</head>

<body onload="Main()">

  

</body>

</html>


Tags

javascript

dialog-box confirm popup alert-types