Sunday, February 17, 2013

JavaScript dialog box and function


JavaScript Dialog Boxes
1.   Alert: To show information to user.
Ex:
      <script language=”javascript”>
                  alert(“You are logged in \n”);
      </script>

2.   Confirm: Confirm user’s choice (True/False).
Ex:
      <script language=”javascript”>
                  confirm(“Do you want to continue ?”);
      </script>

3.   Prompt: To get input from user.
Ex:
      <script language=”javascript”>
                  prompt(“Enter value : ”+”0”);
      </script>

Note: Prompt takes input in form of characters. We can convert character into Int or Float using parseInt() or parseFloat() respectively.


Q. Program to input two values and print their addition.

      <script language=”javascript”>
                  Var a,b,c;
                  a=prompt(“Enter value for a :”+0);
                  b=prompt(“Enter value for b :”+0);
                  c=parseInt(a)+parseInt(b);
                  alert(“Addition of “+a+” & ”+b+” is “+c);
      </script>

Q. Program to print remainder and quociant.

<script language="javascript">
                              var a,b,div,mod;
                              a=prompt("Enter value for a : "+0);
                              b=prompt("Enter value for b : "+0);
                              div=parseInt(a)/parseInt(b);
                              alert("Division of "+a+" & "+b+" is "+parseInt(div));
                              mod=parseInt(a)%parseInt(b);
                              alert("Remainder of "+a+" & "+b+" is "+parseInt(mod));
      </script>



JavaScript Functions
<head>
      <script language="javascript">
                  function msg()
                  {
                              alert(“Function is called”);
                  }                      
                  </script>
      </head>
      <body>
                  <input type=”Button” value=”Function call” onclick=”msg();”/>
      </body>

0 comments:

Post a Comment

Powered by Blogger.