Friday 18 November 2011

Dialog boxes


Javascript provides the ability to pickup user input or display small amount of text to the user by using dialog boxes. These dialog boxes appear as separate windows and their content depends on the information provided by the user.

          In javascript we can create three kind of boxes.

1.Alert dialog box
2.confirm dialog box
3.prompt dialog box

Ø       alert dialog box

          Alert dialog box use to display some textual information on web browser.

Syntax          

                   alert(“message”);

    • The alert dialog box display the string passed to the alert() method, as well as ok button.
    • The javascript and the HTML program, in which this code snippet is held, will not continue processing until the ok button pressed.
Example

<html>
          <body>
                   <script language="javascript">
                             alert(“Hello World”);
</script>
          </body>
      </html>

Ø       confirm dialog box

·         A confirm box is often used if you want the user to verify or accept something.
·         A confirm box display the predefine message and ok & cancel button.
·       When a confirm box pops up, the user will have to click either “OK” or “CANCEL” button to proceed.
·     If the user clicks “OK”, the box returns true. If the user clicks “cancel”, the box returns false.

Syntax          

                   confirm(“message”);

Example

<html>
          <body>
                   <script language="javascript">
                             var a = confirm(“Hello World”);
                             if(a==true)
                             {
                                      document.write(“you have pressed ok button”);
                             }
                             else
                             {
                                      document.write(“you have pressed cancel button”);
                             }       
</script>
          </body>
     </html>

Ø       Prompt dialog box

·         A prompt box is often used if you want the user to input a value before entering a page.
·         A prompt box display a predefine message, a textbox for user input and display OK & Cancel button.
·       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 “OK” the box returns the input value. If the user clicks “cancel” the box returns null.

Syntax          

                   prompt(“some text”);
                        OR
                        prompt(“some text”, ”default value”);

Example

<html>
          <body>
                   <script language="javascript">
                             var a = prompt("Enter your Name : ",”Name”);
                             if(a!='')
                             {
                                      document.write("Hi "+a);
                             }
</script>
          </body>
     </html>

Posted By : Hemangi Zala

No comments:

Post a Comment