Lecture Summary 7

Prepared by: Anthony Larrain


Javascript Functions.


Alert Window

alert1.html alert2.html

In alert2 demo, the body tag has an attribute called onUnLoad. This attribute can be used to specify code that is executed when the user leaves the page.

More Computations with Functions

In each statement what is assigned to variable result

var first, second, result
first  = 2;
second = 3

1)  result = Math.pow(first, second);

2)  result = Math.pow(first, second + second);

3)  result = Math.pow(second, 2);

4)  result = Math.pow(first + 1, second - 3);

5)  result = 3 * Math.pow(first, 2);

6)  result = 3 +  Math.pow(first, 2);

7)  result = 3 +  Math.pow(first, 4) / 4;

8)  result = Math.sqrt(16) + Math.pow(3, 2); 


User Defined Functions.

Event Driven Programming

Example

The code

<html> <head> <title>First Example handling events it130 </title> <script type="text/javascript"> function displayMessage() { alert("Nothing in life is free"); } </script> <head> <body> <form name="frmFreeMoney"> <input type="button" value="Click here for free money" onclick="displayMessage();" /> </form> </body> </html>

TextBoxes

luckynumber.html

Features of Lucky Number


Dice2 Program

Dice Roll Example

Dice 1 Dice 2

The result is   

The Code

<html> <!-- Simulate rolling two die. The output is displayed in textboxes. Author: Anthony Larrain Demonstration It130 --> <head> <title>Dice Simulation 1 , it130 </title> <style> body{ margin:1% 3%; background:linen; } h2 { font-size:1.4em; color:navy; font-family:"book antiqua"; letter-spacing:4px; word-spacing:7px; } th { text-align:left; background:#003300; color:#FFFFCC; padding:5px; } .buttonStyle { background:blue; font-size:1.2em; color:yellow; } </style> <script type="text/javascript"> function roll() { var die1,die2,result; die1 = 1 + Math.floor(Math.random() * 6); die2 = 1 + Math.floor(Math.random() * 6); result = die1 + die2; document.frmDice.txtDie1.value = die1; document.frmDice.txtDie2.value = die2; document.frmDice.result.value = result; } </script> </head> <body> <h2>Dice Roll Example </h2> <form name="frmDice"> <table border="0"> <tr> <th> Dice 1 </th> <th> Dice 2 </th> </tr> <tr> <td><input type="text" name="txtDie1" value=""></td> <td><input type="text" name="txtDie2" value=""></td> </tr> </table> <p><td><input class="buttonStyle" type="button" value="Press to Roll the Dice" onClick="roll();"></td></p> <p>The result is &nbsp;&nbsp; <input type="text" size="5" name="result" value=""></p> </body> </html>

Text Boxes for Getting Input.

Recall we wrote several programs to convert temperatures. Each time we used a prompt window to get the input. The program below uses a text box to get the input.

temperature2.html

Dynamically Changing an Image

     

The Code

<form name="frmImageChange"> <p> <img name="imgAnimal" src="images/bear.jpg" /> </p> <p> <input type="button" value="Lions" onclick="document.images.imgAnimal.src = 'images/lion.jpg';" /> &nbsp; <input type="button" value="Tigers" onclick="document.images.imgAnimal.src = 'images/tiger.jpg';" /> &nbsp; <input type="button" value="Bears" onclick="document.images.imgAnimal.src = 'images/bear.jpg';" /> &nbsp; </p>

@Anthony Larrain 2005 - 2006

Send Questions or Comments to hci201@yahoo.com