4. Events are where it happens! Bear Bibeault Yehuda Katz.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
Tutorial 15 Working with the Event Model. XP Objectives Compare the IE and W3C event models Study how events propagate under both event models Write a.
XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
The Web Wizard’s Guide To JavaScript Chapter 5 More Image Swapping.
Definition from Wikipedia.  The Prototype JavaScript Framework  implemented as a single file of JavaScript code  named prototype.js (
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.
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
Intro to JavaScript Events. JavaScript Events Events in JavaScript let a web page react to some type of input Many different ways to handle events due.
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.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
© 2012, Mike Murach & Associates, Inc.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
Getting a handle on ActionScript A basic primer for non-programmers.
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.
Using Client-Side Scripts to Enhance Web Applications 1.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
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.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Dialog boxes in JavaScript Events in JavaScript – What are they – “Which events are there?” – “How do I register event handlers to an HTML element?” –
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event.
Introduction to Programming the WWW I CMSC Summer 2003 Lecture 13.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
INTRODUCTION JavaScript can make websites more interactive, interesting, and user-friendly.
Event Handling & AJAX IT210 Web Systems. Question How do we enable users to dynamically interact with a website? Answer: Use mouse and keyboard to trigger.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
5. Energizing pages with animations and effects BEAR BIBEAULT YEHUDA KATZ.
Host Objects: Browsers and the DOM
Understanding JavaScript and Coding Essentials Lesson 8.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
Events CS The keyword this  all JavaScript code actually runs inside of an object  by default, code runs inside the global window object  all.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Chapter 6 Murach's JavaScript and jQuery, C6© 2012, Mike Murach & Associates, Inc.Slide 1.
ASP.Net ICallback Vijayalakshmi G M Senior Trainer Binary Spectrum.
Flux & React Web Application Development Mark Repka, Rich McNeary, Steve Mueller.
Jozef Goetz contribution, © by Pearson Education, Inc. All Rights Reserved.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
1 Project 8: Tic Tac Toe. 2 Assignment Write an HTML and JavaScript page that pemits users to play Tic Tac Toe. Both players use a single browser. Alternating.
REEM ALMOTIRI Information Technology Department Majmaah University.
Programming Web Pages with JavaScript
Introduction to JavaScript Events
JavaScript and Events.
14 A Brief Look at JavaScript and jQuery.
Introduction to JavaScript
A second look at JavaScript
jQuery The Easy JavaScript Nikolay Chochev Technical Trainer
Working with the Event Model
Events.
Web Design and Development
JavaScript and Events CS Programming Languages for Web Applications
Introduction to JavaScript
Tutorial 10: Programming with javascript
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
Murach's JavaScript and jQuery (3rd Ed.)
JavaScript and Events CS Programming Languages for Web Applications
Murach's JavaScript and jQuery (3rd Ed.)
Presentation transcript:

4. Events are where it happens! Bear Bibeault Yehuda Katz

Events are where it happens! The event models as implemented by the browsers The jQuery Event Model Binding event handlers to DOM elements The Event object instance Triggering event handlers under script control Registering proactive event handlers

Events are where it happens! A web application using HTML and JavaScript, the program steps are : 1. Set up the user interface. 2. Wait for something interesting to happen. 3. React accordingly. 4. Go to 2.

4.1 Understanding the browser event models Browser Event Model, but most people have come to call it the DOM Level 0 Event Model. For Internet Explorer, even up to IE 8, an API- based proprietary event model that provides a subset of the functionality of the DOM Level 2 Event Model is available.

4.1.1 The DOM Level 0 Event Model

onclick="say('BOOM!');” imageElement.onclick = function(event) { say('BOOM!'); };

4.1.1 The DOM Level 0 Event Model THE EVENT INSTANCE if (!event) event = window.event; var target = (event.target) ? event.target : event.srcElement; (PROPAGATION)

4.1.2 The DOM Level 2 Event Model One severe shortcoming of the DOM Level 0 Event Model is that, because a property is used to store a reference to a function that’s to serve as an event handler, only one event handler per element can be registered for any specific event type at a time. If we have two things that we want to do when an element is clicked, the following statements aren’t going to let that happen: someElement.onclick = doFirstThing; someElement.onclick = doSecondThing;

4.1.2 The DOM Level 2 Event Model ESTABLISHING EVENT HANDLERS Rather than assigning a function reference to an element property, DOM Level 2 event handlers— also termed listeners—are established via an element method. Each DOM element defines a method named addEventListener() that’s used to attach event handlers (listeners) to the element. The format of this method is as follows: addEventListener(eventType,listener,useCapture)

4.1.2 The DOM Level 2 Event Model

EVENT PROPAGATION

4.1.3 The Internet Explorer Event Model Rather than addEventListener(), the Internet Explorer Model defines a method named attachEvent() for each DOM element. This method accepts two parameters similar to those of the standard model: attachEvent(eventName,handler)

4.2 The jQuery Event Model Although it’s true that the creation of highly interactive applications requires a hefty reliance on event handling, the thought of writing event-handling code on a large scale while dealing with the browser differences is enough to daunt even the most intrepid of page authors.

4.2 The jQuery Event Model Provides a unified method for establishing event handlers Allows multiple handlers for each event type on each element Uses standard event-type names: for example, click or mouseover Makes the Event instance available as a parameter to the handlers Provides unified methods for event canceling and default action blocking

4.2.1 Binding event handlers with jQuery Using the jQuery Event Model, we can establish event handlers on DOM elements with the bind() method. Consider the following simple example: $('img').bind('click',function(event){alert('Hi there!');});

4.2.1 Binding event handlers with jQuery $('#thing1').bind('click.editMode',someListener); $('#thing2').bind('click.editMode',someOtherListe ner);... $('#thingN').bind('click.editMode',stillAnotherList ener); By grouping all these bindings into a namespace named editMode, we can later operate upon them as a whole.

4.2.1 Binding event handlers with jQuery $('*').unbind('click.editMode'); This will remove all click bindings (the explanation of the unbind() method is coming up in the next section) in the namespace editMode for all elements on the page.

4.2.1 Binding event handlers with jQuery Before we leave bind(), let’s consider one more example: $('.whatever').bind({ click: function(event) { /* handle clicks */ }, mouseenter: function(event) { /* handle mouseenters */ }, mouseleave: function(event) { /* handle mouseleaves */ } }); For occasions when we want to bind multiple event types to an element, we can do it with a single call to bind(), as shown.

4.2.1 Binding event handlers with jQuery

4.2.2 Removing event handlers

4.2.3 Inspecting the Event instance

Q & A