Page 214 - Web Applications (803) Class 12
P. 214

Example 3— Prompt box
              In this example the function func1( ) is called and executed when the button is clicked. The prompt function
              asks the user to enter a value. In case the value entered is not null (or blank left by user) the respective value
              is displayed in the body of the webpage.

                  <html>

                  <script>
                  function func1() {
                  var age = prompt("Enter your Age","45");
                  if (age != null)

                     document.getElementById("d").innerHTML ="Hello! you are " + age +" years
                      old";
                  }
                  </script>



                  <body style="text-align: center;">
                  <h1 style="color:blue;">
                  Using the Prompt Box in JavaScript

                  </h1>
                  <input type="button" onclick="func1()" value="Click me!">
                  <p id="d"></p>

                  </body> </html>
              Output:














              Anonymous Function
              A function that has no name attached to it is called an anonymous function. To define a function in JavaScript,
              we typically use the function keyword followed by the function name; however, for anonymous functions, we
              only use the function keyword.

              An anonymous function is not accessible once it is created; rather, it is assigned to a variable as a value. The
              function is always invoked (called) using the variable name. Example:

                     <script>
                     var greeting = function () {

                             document.write("Today is a holiday!");
                     };


                212   Touchpad Web Applications-XII
   209   210   211   212   213   214   215   216   217   218   219