Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 71 - 2612_Digimind
P. 71

    When the preceding statement is executed, a prompt box will appear in the web browser asking
                     to input the age. By default, the input taken by the prompt() method is considered as a string in
                     JavaScript.
                     If you want to perform mathematical calculations, you need to convert the value into an integer by
                     using the parseInt() method and into a floating-point number by using the parseFloat() method.
                     We can use the parseInt() method in the following way:

                     var num = parseInt(prompt("Enter first number: "));
                     The document.write() method is used to display output on the web page.
                     For example:

                     document.write("Hello from JavaScript");
                     The preceding statement will display the message "Hello from JavaScript" on the web page. Let us
                     create a web page to calculate the sum of two numbers in JavaScript.

                     <!DOCTYPE HTML>
                     <HTML>
                     <HEAD>
                     <TITLE> Statements in JavaScript </TITLE>
                     </HEAD>
                     <BODY>
                     <SCRIPT type="text/javascript">
                     var a = parseInt(prompt("Enter first number: "));
                     var b = parseInt(prompt("Enter second number: "));
                     var c;
                     c = a + b;
                     document.write("The sum is: ");
                     document.write(c);
                     </SCRIPT>
                     </BODY>

                     </HTML>
                     Save the file with .html extension and open the web page in web browser. You will get the following
                     output:

                                                                                          Dynamic Web Pages in HTML5  69

| 66 67 68 69 70 71 72 73 74 75 76 |