Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and...

45
Event Handling

Transcript of Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and...

Page 1: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Event Handling

Page 2: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Overview

• How to listen (and not listen) for events• Creating a utility library (and why you

should)• Typology of events• Pairing event listeners• Cross-browser compatibility • Event phases (capturing vs. bubbling)• Event delegation vs. event binding

Page 3: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

About Events

• Events happen all the time

• KEY: Determine which events you care about

• Typically triggers a function– Named function or anonymous function

Page 4: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Four Methods For Listening1. Inline event handlers (putting it in the HTML)

2. window.onload = init;

3. window.addEventListener(‘load’, init, false);– Load is the event to be listened for– Init is the function to be called– False refers whether to watch for the Event Phase.

4. window.attachEvent(‘onload’, init); // for IE

Page 5: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Checking for Version

Page 6: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Simplifying Event Handling

• Instead of wrapping this up in if-then conditional, we could just write a custom function and do some checking:

– An event is a type of function– We need to know what type of event– We need to know what function to run

Page 7: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 8: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 9: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 10: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 11: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Creating a Utility Library

• You will be reusing a lot of the functions over and over, as well as listening for different types of events.

• Why not put them into a utility library?

Page 12: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 13: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Instead of writing document.getElementById() every time, you can just use $() to gain access to the id element (similar to jQuery)

See page 159 for a review on typeof evaluator

Page 14: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Make sure to separate your object items with a comma (except for the last one)

Page 15: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 16: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 17: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

You can use the code from earlier to create the add event function. Just copy and paste.

Page 18: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 19: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

You will also want to create a way to stop listening for an event. All you need to do is change the word from add or attach(IE) to remove or detach.(IE)

Page 20: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 21: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Don’t forget your closing semi-colon for the object U.

Page 22: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Event Types

Input Device Browser Keyboard Form

mousedownmouseupclick

mouseoutmouseovermousemove

loadunloadresizescrollcopycutpaste

keydownkeypresskeyup

submitchangefocusblurselect

Many additional events are possible.

http://www.w3schools.com/tags/ref_eventattributes.asp

Page 23: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Example Code Using Utility Library

When a value is changed, call the limitText function

Page 24: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 25: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 26: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Pairing Events

Page 27: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Pairing Events

• Using the SAME function to handle comparable events on the SAME element.

Page 28: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Pairing Events

• Click, mouseover, mouseenter (mousecentric)

• Touch, focus (easier to register with mobile devices)

Page 29: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

BRIEF ASIDE ABOUT TOUCH EVENTS

See Building Touch Interfaces with HTML5 by Stephen Woods for more information

Page 30: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Taps Vs. Clicks

• Touch devices still generate mouse events

• User taps on an element but does not leave finger on the screen– onclick fired 300ms after the tap

Page 31: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Taps• Four types of taps related to touch

1. When touch starts (user puts finger on screen)2. When touchpoint moves (user moves finger

without removing it from screen)3. When touch ends (user takes finger off of the

screen)4. When touch is cancelled (something, such as a

notification, interrupts the touch)

Page 32: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Touch Events in WebKit

Event Name Description Contains Touches Array

touchstart Start of touch Yes

touchmove Touchpoint changes Yes

touchend Touch ends Yes

touchcancen Touch is interrupoted No

Page 33: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Properties of Touch ArrayProperty Descriptionidentifier Unique identifier for this touch. As long as the user keeps

the finger on the screen, this will not change.

screenX The X position of the touch, relative to the left side of the device screen, regardless of scrolling.

screenY The Y position, relative to the top of the screen.

clientX The X position relative to the left side of the browser viewport, in subtracting any browser “chrome”. Basically the same as screenX.

clientY Same as above, except it gets the Y position relative to the top of the viewport

pageX The X position relative to the left of the rendered page.

pageY The Y position relative to the top of the rendered page.

target The element under the touchpoint when the touch is started. If the touch moves over a different element, this value does not change.

Page 34: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Referencing an Event

• Problem

– You want to listen for the same event (e.g., click) but want to use it on different elements (e.g., image, link).

Page 35: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Referencing an Event

• Solution!

– Reference the event!!!

– IE and other browsers handle this differently

Page 36: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Comparison of Browsers

Page 37: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

Solution

Page 38: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

• Why care about the event?

• It gives us an event object, which contains useful property information– type property (tells us what event

occurred, such as click, resize, blur, etc.)

Page 39: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 40: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 41: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

PREVENTING DEFAULT BEHAVIOR

Page 42: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 43: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Page 44: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.

EVENT PHASES

Page 45: Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.