JavaScript dialogue box


JavaScript dialogue box

JavaScript supports three important types of dialog boxes. These chat boxes can be used to amplify and alert, get a confirmation for any input, or have some type of input from users.

Dialog box notification

A warning dialog box is widely used to give a warning message to users. For example, if one input field needs to enter a specific text but the user does not provide the input, you can use the alert box to provide a warning message as part of the verification.

For example

Live Demo
<html>
   <head>
      <script type = "text / javascript">
         <! -
            function Warn () {
               warning ("This is a warning message!");
               document.write ("This is a warning message!");
            }
         // ->
      </script>
   </head>
   
   <body>
      <p> Click the next button to see the result: </p>
      <form>
         <input type = "button" value = "Click Me" onclick = "Warn ();" />
      </form>
   </body>
</html>

Verification dialog box

The check box is widely used to take user permission for any option. Displays the dialog box with two buttons: OK and cancel.

When a user clicks the OK button, the window method confirms () it will come back true. When a user clicks the Cancel button, then confirm () returns the lie. You can use the confirmation dialog box next.

For example

Live Demo

<html>
   <head>
     <script type = "text / javascript">
         <! -
            job getConfirmation () {
               var retVal = confirm ("Do you want to continue?");
               if (retVal == true) {
                  document.write ("User wants to continue!");
                  true return;
               } more {
                  document.write ("User does not want to continue!");
                  return false;
               }
            }
         // ->
      </script>
   </head>
   
   <body>
      <p> Click the next button to see the result:


      <form>
         <input type = "button" value = "Click Me" onclick = "getConfirmation ();" />
      </form>
   </body>
</html>

Instant Chat Box

A quick chat box is very useful if you want to open a text box to get user input. Therefore, it enables you to communicate with the user. User needs to fill in the field and click OK.

This dialog is displayed using a prompt () method that takes two parameters: (i) the label you want to display in the text box and (ii) the default string to be displayed in the text box.

The following example illustrates how to use a quick dialog -

Live Demo
<html>
   <head>
     <script type = "text / javascript">
         <! -
            job getValue () {
               var retVal = immediately ("Enter your name:", "your name here");
               document.write ("Install:" + retVal);
            }
         // ->
      </script>
   </head>
   
   <body>
      <p> Click the next button to see the result:


      <form>
         <input type = "button" value = "Click Me" onclick = "getValue ();" />
      </form>
   </body>
</html>

Example :

<html> <body> <h2>Knowledge2life</h2> <button onclick="myFunction()">Try it</button> <script> function myFunction() { alert("Knowledge2life"); } </script> </body> </html>

OUTPUT:

Knowledge2life