CHAPTER 6 EVENTS WHAT IS AN EVENT Events
- Slides: 56
CHAPTER 6 EVENTS
WHAT IS AN EVENT?
Events are the browser’s way of saying, “Hey, this just happened. ”
When an event fires, your script can then react by running code (e. g. a function).
By running code when an event fires, your website responds to the user’s actions. It becomes interactive.
DIFFERENT EVENT TYPES
USER INTERFACE EVENTS load unload error resize scroll
KEYBOARD EVENTS keydown keyup keypress
MOUSE EVENTS click dblclick mousedown mouseup mouseover mouseout
FOCUS EVENTS focus / focusin blur / focusout
FORM EVENTS input change submit reset cut copy paste select
HOW EVENTS TRIGGER JAVASCRIPT CODE
1
1 Select the element node(s) the script should respond to
1 Select the element node(s) the script should respond to 2
1 2 Select the element node(s) the script should respond to Indicate the event on the selected node(s) that will trigger a response
1 2 Select the element node(s) the script should respond to Indicate the event on the selected node(s) that will trigger a response 3
1 2 3 Select the element node(s) the script should respond to Indicate the event on the selected node(s) that will trigger a response State the code you want to run when the event occurs
BINDING AN EVENT TO AN ELEMENT
There are three ways to bind an event to an element: HTML event handler attributes Traditional DOM event handlers DOM Level 2 event listeners
The following examples show a blur event on an element stored in a variable called el that triggers a function called check. Username().
HTML EVENT HANDLER ATTRIBUTES (DO NOT USE) <input type=“text” id=“username” onblur=“check. Username()”>
HTML EVENT HANDLER ATTRIBUTES (DO NOT USE) ELEMENT <input type=“text” id=“username” onblur=“check. Username()”>
HTML EVENT HANDLER ATTRIBUTES (DO NOT USE) <input type=“text” id=“username” onblur=“check. Username()”> EVENT
HTML EVENT HANDLER ATTRIBUTES (DO NOT USE) <input type=“text” id=“username” onblur=“check. Username()”> FUNCTION
TRADITIONAL DOM EVENT HANDLERS el. onblur = check. Username();
TRADITIONAL DOM EVENT HANDLERS el. onblur = check. Username(); ELEMENT
TRADITIONAL DOM EVENT HANDLERS el. onblur = check. Username(); EVENT
TRADITIONAL DOM EVENT HANDLERS el. onblur = check. Username(); FUNCTION
EVENT LISTENERS el. add. Event. Listener(‘blur’, check. Username, false);
EVENT LISTENERS el. add. Event. Listener(‘blur’, check. Username, false); ELEMENT
EVENT LISTENERS el. add. Event. Listener(‘blur’, check. Username, false); EVENT
EVENT LISTENERS el. add. Event. Listener(‘blur’, check. Username, false); FUNCTION
EVENT LISTENERS el. add. Event. Listener(‘blur’, check. Username, false); BOOLEAN (OPTIONAL)
Because you cannot have parentheses after the function names in event handlers or listeners, passing arguments requires a workaround.
PARAMETERS WITH EVENT LISTENERS el. add. Event. Listener(‘blur’, function() { check. Username(5); }, false);
PARAMETERS WITH EVENT LISTENERS el. add. Event. Listener(‘blur’, function() { check. Username(5); }, false); An anonymous function is used as the second argument.
PARAMETERS WITH EVENT LISTENERS el. add. Event. Listener(‘blur’, function() { check. Username(5); }, false); Inside the anonymous function, a named function is called.
IE 5 - 8 had a different event model and did not support add. Event. Listener() but you can provide fallback code to make event listeners work with older versions of IE.
SUPPORTING OLDER VERSIONS OF IE if (el. add. Event. Listener) { el. add. Event. Listener(‘blur’, function() { check. Username(5); }, false); } else { el. attach. Event(‘onblur’, function() { check. Username(5); }
SUPPORTING OLDER VERSIONS OF IE if (el. add. Event. Listener) { el. add. Event. Listener(‘blur’, function() { check. Username(5); }, false); } else { el. attach. Event(‘onblur’, function() { check. Username(5); }
EVENT FLOW
HTML elements nest inside other elements. If you hover or click on a link, you will also be hovering or clicking on its parent elements.
EVENT BUBBLING
EVENT CAPTURING
THE EVENT OBJECT
When an event occurs, the event object can tell you information about it and which element it happened upon.
PROPERTIES target type cancelable METHODS prevent. Default() stop. Propagation()
ELEMENT AN EVENT OCCURRED ON 1: EVENT LISTENER CALLS FUNCTION function check. Username(e) { var target = e. target; } var el = document. get. Element. By. Id(‘username’); el. add. Event. Listener(‘blur’, check. Username, false);
ELEMENT AN EVENT OCCURRED ON 2: EVENT OBJECT PASSED TO FUNCTION function check. Username(e) { var target = e. target; } var el = document. get. Element. By. Id(‘username’); el. add. Event. Listener(‘blur’, check. Username, false);
ELEMENT AN EVENT OCCURRED ON 3: ELEMENT THAT EVENT HAPPENED ON function check. Username(e) { var target = e. target; } var el = document. get. Element. By. Id(‘username’); el. add. Event. Listener(‘blur’, check. Username, false);
EVENT DELEGATION
Creating event listeners for a lot of elements can slow down a page, but event flow allows you to listen for an event on a parent element.
Placing an event listener on a container element: Works with new elements Solves limitations with the this keyword Simplifies code
- Sentinel incident meaning
- Simple or compound event
- Independent or dependent
- Independent event vs dependent event
- Swot event management
- Bridge breaks in central java killing 5
- Moscow a russian journalist has uncovered evidence
- Mutually exclusive events vs not mutually exclusive events
- Summary to kill a mockingbird chapter 12
- Summary of chapter 17 things fall apart
- Lord of the flies chapters 4-6 summary
- Lord of the flies chapter 9 summary
- Summary of chapter 4 lotf
- Chapter 11 lotf summary
- The great gatsby chapter 7 summary
- Chapter 9 of lord of the flies
- Chapter 2 events
- Last day events chapter 6
- Gynastic event
- Graylog sysmon
- Event forwarding gpo
- Favourite event
- Provision of food and service for a special event
- Evaluate this sql statement
- Types of categories
- Cem in pharmacovigilance
- Example of rea
- Organizing committee structure for an event
- A symbol is an object action or event
- Bottleneck effect example
- Sequential linear menus
- Kaizen optimizer
- What is an event triangle
- Probability odds
- Clk'event
- Staged event driven architecture
- Simple event correlator examples
- Scentsy sales tax
- Royalty
- Risk event life cycle
- Banquet event order template
- How to write report of an event
- Flis reportable events
- To retell the past event
- Contoh time sampling
- Proc logistic event
- Independent probability
- The likelihood that an event will occur.
- Simple or compound event
- Compound events formula
- Event recording data sheet
- Narrative paragraph form
- Event driven programming paradigm
- Event recording data sheet
- Event oriented thinking
- Fabric event listener
- C++ event handler