JavaScript | Dialogue Boxes

JavaScript uses 3 kind of dialog boxes : ALERT, PROMPT and CONFIRM. These dialog boxes can be of very much help for making our website look more attractive.

ALERT BOX :

An alert box is used in the website to show a warning message to the user that they have entered the wrong value other than what is required to filled in that position. Nonetheless, an alert box can still be used for friendlier messages. Alert box gives only one button “OK” to select and proceed.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   <head>
         
      <scrip</div>t type="text/javascript">
         <!--
            function Warning() {
               alert ("Warning danger you have not filled everything");
               document.write ("Warning danger you have not filled everything");
            }
      </script>
        
   </head>
   <body>
      <p>Click me </p>
        
      <form>
         <input type="button" value="Click Me" onclick="Warning();" />
      </form>
        
   </body>
</html>

chevron_right


Output :



CONFIRM BOX :

A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either “OK” or “Cancel” to proceed. If the user clicks on the OK button, the window method confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false and will show null.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   <head>
     
      <script type="text/javascript">
  
            function Confirmation(){
               var Val = confirm("Do you want to continue ?");
               if( Val == true ){
                  document.write (" CONTINUED!");
                  return true;
               }
               else{
                  document.write ("NOT CONTINUED!");
                  return false;
               }
            }
  
      </script>
        
   </head>
   <body>
      <p>Click me: </p>
        
      <form>
         <input type="button" value="Click Me" onclick="Confirmation();" />
      </form>
        
   </body>
</html>

chevron_right


Output :

PROMPT BOX :

A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value. If the user clicks the OK button, the window method prompt() will return the entered value from the text box. If the user clicks the Cancel button, the window method prompt() returns null.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   <head>
        
      <script type="text/javascript">
           
            function Value(){
               var Val = prompt("Enter your name : ", "name");
               document.write("You entered : " + Val);
            }
    
      </script>
        
   </head>
     
   <body>
      <p>Click me: </p>
        
      <form>
         <input type="button" value="Click Me" onclick="Value();" />
      </form>
        
   </body>
</html>

chevron_right


Output :

LINE BREAKER :

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<p>Line-Breaks</p>
  
<button onclick="alert('GEEKSFOR\nGEEKS')">CLICK ME</button>
  
</body>
</html>

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.