Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.

Similar presentations


Presentation on theme: "JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells."— Presentation transcript:

1 JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells browsers what code to run in response to the specified event. To insert an event handler as an HTML attribute, use the HTML code: Example:

2 JavaScript Events 2

3 Two Important JavaScript Objects The Date object contain information about a specified date and time. The Math object is an object that can be used for performing mathematical tasks and storing mathematical values. 3

4 Creating a Date Object To store a date and time in a variable, use the JavaScript command variable = new Date("month day, year hours:minutes:seconds"); where variable is the name of the variable that contains the date object, and month, day, year, hours (24-hour clock), minutes, and seconds indicate the date and time to be stored in the object. To create a date object containing the current date and time, use the constructor: variable = new Date(); 4

5 Get and Date Methods Get and Set Date Methods 5 Date methods can be used to get information from a date object or to set a date object’s values

6 The Set Date Methods 6

7 Math Methods Math methods are functions used for performing advanced calculations and mathematical operations such as: 7

8 Constants in the Math Object 8 The Math object also stores numeric values for mathematical constants:

9 Operators in JavaScript An operator is a symbol used to act upon an item or a variable within a JavaScript expression. The variables or expressions that operators act upon are called operands. Types of operators: – Arithmetic- Logical –Assignment- Conditional –Comparison 9

10 Binary Arithmetic Operators 10

11 Some Other Arithmetic Operators Increment operator: increases the value of the operand by 1. Example: x++; Decrement operator: decreases the value of the operand by 1. Example: x--; Negation operator: changes the sign of an item’s value Example: -x 11

12 Assignment Operators 12

13 Comparison Operators Comparison Operators 13

14 Logical Operators Logical operators allow you to connect several expressions together. 14

15 Conditional Operators A conditional operator performs a comparison test and assigns one value if the condition is true and another value if the condition is false. Example: var ampm = (hour < 12) “AM” : “PM” ; 15

16 A Complete Example: Putting It All Together A Complete Example: Putting It All Together Let’s create a web page that simulates rolling a pair of dice. 1.Display a button on the screen. 2.When the user presses the button (an event), use the Math object to generate a random number between 1 and 6. Do this twice. 3.Display the value of the dice roll in an alert popup on the web page. Save this file! We’ll be adding more to the web page next week. 16


Download ppt "JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells."

Similar presentations


Ads by Google