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.

Slides:



Advertisements
Similar presentations
5.1 JavaScript Execution Environment
Advertisements

The jQuery library. What is jQuery ? A javascript lightweight library Is very easy to use Is powerful Is cross-browser compatible Downloadable from jQuery.com,
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
The Web Warrior Guide to Web Design Technologies
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Agenda Assignment one graded –Did not see any big issues »Some still have issues.
JavaScript, Third Edition
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Agenda Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript.
XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.
Spring /6.831 User Interface Design and Implementation1 Lecture 26: Mobile User Interfaces.
CP476 Internet Computing JavaScript and HTML1 1.JavaScript Execution Environment The JavaScript Window object represents the window in which the browser.
INTRODUCTION TO HTML5 Drag and Drop in HTML5. Browsers Support  Currently, most of the major HTML5 desktop web browsers support the new HTML5 drag-and-drop.
Lab 9 – User Forms Design. User Forms What are user forms? –Known as dialog boxes –Major ways for getting user input An example of using user forms: Monthly.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Chapter 5 © 2003 by Addison-Wesley, Inc. 1 Chapter 5 JavaScript and HTML Documents.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
Chapter 5 JavaScript and HTML Documents. © 2006 Pearson Addison-Wesley. All rights reserved JavaScript Execution Environment - The JavaScript.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
Chapter 5 © 2012 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
1 Goals and Objectives Goals Goals Understand how JavaScript makes it possible to interact with web pages, minimizes client/server traffic, enables verification.
Lecture Set 13 Drawing Mouse and Keyboard Events Part B – Mouse and Keyboard Events.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Chapter 5 © 2005 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
Thursday, August 6 th, 2015 Instructor: Craig Duckett Event Handling.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 12.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
H.Melikyan/4910/031 Programming the World Wide Web JavaScript Dr.Hayk Melikyan Departmen of Mathematics and CS JavaScript and HTML Documents.
Host Objects: Browsers and the DOM
MTA EXAM HTML5 Application Development Fundamentals.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Modern JavaScript Develop And Design Instructor’s Notes Chapter 8 – Event Handling Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
CHAPTER 7 JQUERY WHAT IS JQUERY? jQuery is a script. It is written in JavaScript.
 ASP.NET provides an event based programming model that simplifies web programming  All GUI applications are incomplete without enabling actions  These.
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
CSE 154 LECTURE 10: MORE EVENTS. Problems with reading/changing styles Click Me HTML window.onload = function() { document.getElementById("clickme").onclick.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
4.5. Respond to the touch interface 4.6. Code additional HTML5 APIs 4.7. Access device and operating system resources JavaScript Coding for the Touch Interface,
04 | Code by Using JavaScript Christopher Harrison | Content Developer, Microsoft Rachel Jones| Microsoft Certified Trainer, SourceDev.
Chapter 6 © 2003 by Addison-Wesley, Inc Introduction - Dynamic HTML is not a new markup language - A dynamic HTML document is one whose tag attributes,
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
Scrolling Down vs. Multiple Pages
Introduction to JavaScript Events
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and Events.
CSE 154 Lecture 11: More Events.
Events.
Introduction to jQuery
E-commerce Applications Development
JavaScript and Events CS Programming Languages for Web Applications
CSE 337 Lecture 10: Events.
CS7026 jQuery Events.
Lecture 11: Keyboard Events
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
JavaScript and Events CS Programming Languages for Web Applications
Events Part III The event object.
Presentation transcript:

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 Cross-browser compatibility Event phases (capturing vs. bubbling) Event delegation vs. event binding

About Events Events happen all the time KEY: Determine which events you care about Typically triggers a function – Named function or anonymous function

Four Methods For Listening 1.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

Checking for Version

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

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?

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

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

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

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)

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

Event Types Input DeviceBrowserKeyboardForm mousedown mouseup click mouseout mouseover mousemove load unload resize scroll copy cut paste keydown keypress keyup submit change focus blur select Many additional events are possible.

Example Code Using Utility Library When a value is changed, call the limitText function

Pairing Events

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

Pairing Events Click, mouseover, mouseenter (mousecentric) Touch, focus (easier to register with mobile devices)

BRIEF ASIDE ABOUT TOUCH EVENTS See Building Touch Interfaces with HTML5 by Stephen Woods for more information

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

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)

Touch Events in WebKit Event NameDescriptionContains Touches Array touchstart Start of touchYes touchmove Touchpoint changesYes touchend Touch endsYes touchcancen Touch is interrupotedNo

Properties of Touch Array PropertyDescription identifier 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.

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).

Referencing an Event Solution! – Reference the event!!! – IE and other browsers handle this differently   

Comparison of Browsers

Solution

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.)

PREVENTING DEFAULT BEHAVIOR

EVENT PHASES