web development -2
HTML5, CSS3, and JavaScript
6th Edition
Working with Events and Styles
Tutorial 11
Carey
XP
XP
XP
XP
XP
Objectives
Create an event handler
Reference an attribute from a page element
Change the inline style of a page element
Create code for mouse events
Create code for keyboard events
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
2
XP
XP
XP
XP
XP
2
Objectives (continued)
Design and apply custom cursors
Create and apply anonymous functions
Work with alert, confirm, and prompt dialog boxes
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
3
XP
XP
XP
XP
XP
3
Introducing JavaScript Events
JavaScript programs run in response to events
Events: Actions initiated by the user or by the browser
Example:
Clicking an object on a form
Closing a web page
JavaScript events can be used to build a Hanjie puzzle, i.e., a grid in which each grid cell is either filled or left empty
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
4
XP
XP
XP
XP
XP
4
Introducing JavaScript Events (continued)
The Hanjie puzzle app contains three puzzles named puzzle1, puzzle2, and puzzle3
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
5
XP
XP
XP
XP
XP
5
Creating an Event Handler
Event handler: A property that controls how an object will respond to an event
Event handler waits until the event occurs and then responds by running a function or command block to execute an action
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
6
XP
XP
XP
XP
XP
6
Creating an Event Handler (continued 1)
Event handlers can be added to a page element using the following attribute:
<element onevent = “script”>
where element is the element in which the event occurs, event is the name of the event, and script are the commands that the browser runs in response to the event
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
7
XP
XP
XP
XP
XP
7
Creating an Event Handler (continued 2)
Event handlers can also be defined as object properties using the command
object.onevent = function;
where object is the object in which the event occurs, event is the name of the event, and function is the name of a function run in response to the event
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
8
XP
XP
XP
XP
XP
8
Creating an Event Handler (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
9
XP
XP
XP
XP
XP
9
Creating an Event Handler (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
10
XP
XP
XP
XP
XP
10
Creating an Event Handler (continued 5)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
11
XP
XP
XP
XP
XP
11
Creating an Event Handler (continued 6)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
12
Add an event handler to the buttons in the Figure 11-8
Apply the following onclick event handler to respond to a mouse click:
object.onclick = function;
where object is the page element that is being clicked and function is the function run in response to the click event
XP
XP
XP
XP
XP
12
Creating an Event Handler (continued 7)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
13
XP
XP
XP
XP
XP
13
Creating an Event Handler (continued 8)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
14
The challenge lies in determining which button was clicked
There is no way of knowing which puzzle to load into the page without knowing which button activated the onclick event handler
This information can be determined using the event object
XP
XP
XP
XP
XP
14
Using the Event Object
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
15
Event object is an object that contains properties and methods associated with an event
Example: The action of clicking a mouse button generates an event object containing information such as which mouse button was clicked, where in the page it was clicked, the time at which it was clicked, and so forth
XP
XP
XP
XP
XP
15
Using the Event Object (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
16
Event object is passed as an argument to the function handling the event to retrieve the information contained in the event object
Example:
function myFunction(evt) {
function code
}
where evt is the name assigned to the parameter receiving the event object from the event handler
XP
XP
XP
XP
XP
16
Using the Event Object (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
17
XP
XP
XP
XP
XP
17
Using the Event Object (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
18
XP
XP
XP
XP
XP
18
Exploring Object Properties
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
19
JavaScript object properties that mirror HTML attributes follow certain conventions
All JavaScript properties must begin with a lowercase letter
If the HTML attribute consists of multiple words, JavaScript property follows a format known as camel case, i.e., the initial word is in lowercase and the first letter of each subsequent word is in uppercase
XP
XP
XP
XP
XP
19
Exploring Object Properties (continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
20
If the name of the HTML attribute is a reserved JavaScript name or keyword, the corresponding JavaScript property is often prefaced with the text string html
class attribute is an exception to this convention because the class name is reserved by JavaScript for other purposes
References to the HTML class attribute use the className property
XP
XP
XP
XP
XP
20
Object Properties and Inline Styles
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
21
Inline styles for each page element can be applied using the following style attribute:
<element style = “property:value”> …
where
element is the page element
property is a CSS style property
value is the value assigned to that property
XP
XP
XP
XP
XP
21
Object Properties and Inline Styles (continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
22
The equivalent command in JavaScript is
object.style.property = “value”;
with the property style written in camel case
Inline styles have precedence over style sheets
XP
XP
XP
XP
XP
22
Creating Object Collections with CSS Selectors
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
23
Change the background color of all table cells by applying the following CSS style rule for all td elements:
table#hanjieGrid td {
background-color: rgb(233, 207, 29);
}
XP
XP
XP
XP
XP
23
Creating Object Collections with CSS Selectors (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
24
In JavaScript, to change the background color of all table cells, you must first define an object collection based on a CSS selector using the following querySelectorAll() method:
document.querySelectorAll(selector)
where selector is the CSS selector that the object collection is based on
XP
XP
XP
XP
XP
24
Creating Object Collections with CSS Selectors (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
25
Once the object collection has been defined, change the background-color style of each td element by applying the backgroundColor property to the objects in the object collection
Reference only the first element that matches a selector pattern using the following JavaScript method:
document.querySelector(selector)
where selector is a CSS selector
XP
XP
XP
XP
XP
25
Creating Object Collections with CSS Selectors (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
26
XP
XP
XP
XP
XP
26
Creating Object Collections with CSS Selectors (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
27
XP
XP
XP
XP
XP
27
Working with Mouse Events
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
28
JavaScript supports events associated with the mouse such as clicking, right-clicking, double-clicking, and moving the pointer over and out of page elements
XP
XP
XP
XP
XP
28
Working with Mouse Events (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
29
A mouse action can be comprised of several events
The action of clicking the mouse button is comprised of three events, fired in the following order:
mousedown (the button is pressed down)
mouseup (the button is released)
click (the button has been pressed and released)
XP
XP
XP
XP
XP
29
Working with Mouse Events (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
30
The event object for mouse events has a set of properties that can be used to give specific information about the state of the mouse
XP
XP
XP
XP
XP
30
Working with Mouse Events (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
31
XP
XP
XP
XP
XP
31
Introducing the Event Model
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
32
Event model: Describes how events and objects interact within the web page and web browser
The process in which a single event is applied to a hierarchy of objects is part of the event model
XP
XP
XP
XP
XP
32
Introducing the Event Model (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
33
Once an event has been initiated, it propagates through the object hierarchy in three phases
Capture phase: The event moves down the object hierarchy starting from the root element (the browser window) and moving inward until it reaches the object that initiated the event
Target phase: The event has reached the target of the event object and no longer moves down the object hierarchy
XP
XP
XP
XP
XP
33
Introducing the Event Model (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
34
Bubbling phase: The event propagates up the object hierarchy back to the root element (browser window) where the propagation stops
XP
XP
XP
XP
XP
34
Introducing the Event Model (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
35
Limitations of event handlers
Event handlers, such as onclick and onmousedown, respond to events during the target phase, but they do not recognize the propagation of events through the capture and bubbling phases
Only one function can be applied to an event handler at a time
XP
XP
XP
XP
XP
35
Adding an Event Listener
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
36
Event listener: Listens for events as they propagate through the capture, target, and bubble phases, allowing the script to respond to an event within any phase
Unlike event handlers, more than one function can be applied to an event using event listeners
XP
XP
XP
XP
XP
36
Adding an Event Listener (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
37
Add an event listener to an object by applying the addEventListener()method
object.addEventListener(event, function [, capture = false]);
where
object is the object in which the event occurs
event is the event
function is the function that is run in response to the event
XP
XP
XP
XP
XP
37
Adding an Event Listener (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
38
capture is an optional Boolean value
true indicates that the function is executed during the capture phase
false (the default) indicates that the function is run during the bubbling phase
XP
XP
XP
XP
XP
38
Removing an Event Listener
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
39
The event model allows to remove event listeners from the document by applying removeEventListener() method object.removeEventListener(event, function [, capture = false]);
where object, event, function, and capture have the same meanings as the addEventListener() method
XP
XP
XP
XP
XP
39
Removing an Event Listener (continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
40
XP
XP
XP
XP
XP
40
Controlling Event Propagation
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
41
The browser has its own default responses to events
Apply the following preventDefault() method to the event object to prevent the occurrence of the browser’s default actions:
evt.preventDefault()
XP
XP
XP
XP
XP
41
Controlling Event Propagation (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
42
Alternatively, you can prevent the browser’s default action by returning the value false from the event handler function
The return false; statement does not prevent default actions if event listeners are used in place of event handlers
XP
XP
XP
XP
XP
42
Controlling Event Propagation (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
43
XP
XP
XP
XP
XP
43
Exploring Keyboard Events
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
44
JavaScript supports the keydown, keypress, and keyup events that allow users to interact with the web page and browser through the keyboard
XP
XP
XP
XP
XP
44
Exploring Keyboard Events (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
45
The keydown and keypress events are similar in name; the difference between them is as follows:
The keydown and keyup events are fired in response to the physical act of pressing the key down and of a key moving up when it is no longer held down
The keypress event is fired in response to the computer generating a character
XP
XP
XP
XP
XP
45
Exploring Keyboard Events (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
46
XP
XP
XP
XP
XP
46
Exploring Keyboard Events (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
47
The value associated with a key event property is affected by the event itself
For the keypress event, the charCode, keyCode, and which properties all return a Unicode character number
Sample values of the charCode, keyCode, which, and key properties for different keyboard keys and events are shown in Figure 11-29
XP
XP
XP
XP
XP
47
Exploring Keyboard Events (continued 4)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
48
XP
XP
XP
XP
XP
48
Exploring Keyboard Events (continued 5)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
49
Modifier keys: Alt, Ctrl, Shift, and Command keys
In addition to character keys, JavaScript supports the modifier keys through the use of the altKey, ctrlKey, shiftKey, and metaKey properties
XP
XP
XP
XP
XP
49
Exploring Keyboard Events (continued 6)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
50
XP
XP
XP
XP
XP
50
Changing the Cursor Style
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
51
Cursors can be defined using the following CSS cursor style:
cursor: cursorTypes;
where cursorTypes is a comma-separated list of cursor types
XP
XP
XP
XP
XP
51
Changing the Cursor Style (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
52
JavaScript command to define cursors is as follows:
object.style.cursor = cursorTypes;
where object is the page object that will display the cursor style when hovered over by the mouse pointer
XP
XP
XP
XP
XP
52
Changing the Cursor Style (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
53
Create a customized cursor from an image file using url(image)where image is an image file
Example: cursor: url(jpf_pencil.png), pointer;
XP
XP
XP
XP
XP
53
Changing the Cursor Style (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
54
By default, the click point for a cursor is located in the top-left corner of the cursor image at the coordinates (0, 0)
Specify a different location by adding the (x, y) coordinates of the click point to the cursor definition as follows:
url(image) x y
where x is the x-coordinate and y is the y-coordinate of the click point in pixels
XP
XP
XP
XP
XP
54
Working with Functions as Objects
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
55
Everything in JavaScript is an object, including functions
Anything that can be done with an object can be done with a function, including
Storing a function as variable
Storing a function as an object property
XP
XP
XP
XP
XP
55
Working with Functions as Objects (continued)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
56
Using one function as a parameter in another function
Nesting one function within another function
Returning a function as the result of another function
Modifying the properties of a function
XP
XP
XP
XP
XP
56
Function Declarations and Function Operators
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
57
The following hello() function is created using the function declaration format:
function hello(){
alert(“Welcome to Hanjie!”);}
Function operator: The definition of the function becomes the variable’s “value”
var hello = function (){
alert(“Welcome to Hanjie!”);
}
XP
XP
XP
XP
XP
57
Function Declarations and Function Operators (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
58
The two ways of defining the hello() function differ in how they are stored
Functions defined with a function declaration are created and saved as the browser parses the code prior to the script being run
Since the function is already stored in memory, the statements that run the function can be placed prior to the statement that declares the function
XP
XP
XP
XP
XP
58
Function Declarations and Function Operators (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
59
Function operators are evaluated as they appear in the script after the code has been parsed by the browser
Function operators are more flexible than function declarations, allowing a function to be placed anywhere a variable can be placed
XP
XP
XP
XP
XP
59
Anonymous Functions
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
60
Anonymous function has function declaration without the function name
Example: The following structure is an anonymous function:
function() {
commands
}
An anonymous function can be inserted into any expression where a function reference is required
XP
XP
XP
XP
XP
60
Anonymous Functions (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
61
Functions that are named are called named functions
Anonymous functions are more concise and easier to manage because the function is directly included with the expression that invokes it
Anonymous functions limit the scope of the function to exactly where it is needed
XP
XP
XP
XP
XP
61
Anonymous Functions (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
62
XP
XP
XP
XP
XP
62
Passing Variable Values into Anonymous Functions
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
63
JavaScript supports two types of variables:
Global variables: Declared outside of any function and thus are accessible throughout the app
Local variables: Declared within a function and are only accessible to code within that function
XP
XP
XP
XP
XP
63
Passing Variable Values into Anonymous Functions (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
64
Global variables should be avoided when possible because
global variables are accessible to every function in the application
the task of tracking which functions are using and modifying the global variables becomes increasingly difficult as the application grows in size and complexity
XP
XP
XP
XP
XP
64
Passing Variable Values into Anonymous Functions (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
65
An advantage of using anonymous functions is that they reduce the need for global variables because they perform their actions locally within a function
One of the challenges of anonymous functions is keeping track of all of the nested levels of functions and procedures
XP
XP
XP
XP
XP
65
Passing Variable Values into Anonymous Functions (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
66
XP
XP
XP
XP
XP
66
Displaying Dialog Boxes
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
67
Alert dialog box can be created using the following alert() method:
alert(text)
where text is the message displayed in the alert dialog box
When an alert dialog box is displayed, the execution of the program code halts until the user clicks the OK button in the dialog box
XP
XP
XP
XP
XP
67
Displaying Dialog Boxes (continued 1)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
68
JavaScript supports confirmation and prompt dialog boxes
XP
XP
XP
XP
XP
68
Displaying Dialog Boxes (continued 2)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
69
XP
XP
XP
XP
XP
69
Displaying Dialog Boxes (continued 3)
New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition
70
XP
XP
XP
XP
XP
70