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.

Slides:



Advertisements
Similar presentations
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Advertisements

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.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Tutorial 16 Working with Dynamic Content and Styles.
Tutorial 10 Programming with JavaScript
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Tutorial 11 Working with Operators and Expressions
Tutorial 14 Working with Forms and Regular Expressions.
Tutorial 13 Working with Objects and Styles. XP Objectives Learn about objects and the document object model Reference documents objects by ID, name,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 10.
XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Events.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
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.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
Creating an Animated Web Page
Working with Objects Creating a Dynamic Web Page.
XP Tutorial 4 New Perspectives on JavaScript, Comprehensive1 Working with Objects Creating an Animated Web Page.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
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.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
DHTML: Working with Objects Creating a Dynamic Web Page.
XP Tutorial 6 New Perspectives on JavaScript, Comprehensive1 Working with Windows and Frames Enhancing a Web Site with Interactive Windows.
XP Tutorial 16 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
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.
The Web Wizard’s Guide To DHTML and CSS Chapter 6 Understanding Events.
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.
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.
Host Objects: Browsers and the DOM
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.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
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.
Introduction to JavaScript DOM Instructor: Sergey Goldman.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
THE DOM.
JavaScript and HTML Simple Event Handling 11-May-18.
Introduction to JavaScript Events
JavaScript and Events.
JavaScript and HTML Simple Event Handling 19-Sep-18.
13 JavaScript: Events.
CSE 154 Lecture 11: More Events.
Working with the Event Model
WEB PROGRAMMING JavaScript.
Working with the Event Model
Events.
JavaScript and Events CS Programming Languages for Web Applications
CS7026 jQuery Events.
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and Events CS Programming Languages for Web Applications
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

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 cross-browser function to capture and remove event handlers Study the properties of the event object Reference the event object under both event models New Perspectives on HTML, XHTML, and Dynamic HTML, 4e2

XP Objectives Retrieve information about the mouse pointer Work with the cursor style Capture keyboard events Halt the propagation of events under both event models Prevent the default action associated with an event New Perspectives on HTML, XHTML, and Dynamic HTML, 4e3

XP Introducing the Event Model New Perspectives on HTML, XHTML, and Dynamic HTML, 4e4

XP Introducing the Event Model An anonymous function is a function without a name – Needs to be run only once The event model describes how events interact with objects – IE event model Supported by IE and Opera – W3C event model Supported by other major browsers New Perspectives on HTML, XHTML, and Dynamic HTML, 4e5

XP Introducing the Event Model In Internet Explorer, event bubbling is when an event is initiated at the bottom of the object tree and rises to the top of the hierarchy In event capturing, events are initiated at the top of the object hierarchy and drop down the object tree to the lowest object – Not supported in the IE event model New Perspectives on HTML, XHTML, and Dynamic HTML, 4e6

XP Introducing the Event Model New Perspectives on HTML, XHTML, and Dynamic HTML, 4e7 Event bubbling in the IE event model

XP Introducing the Event Model New Perspectives on HTML, XHTML, and Dynamic HTML, 4e8 Event capturing

XP Introducing the Event Model In the W3C event model, an event is split into three phases – A capture phase as the event moves down the object hierarchy – A target phase in which the event reaches the object from which the event originated – A bubbling phase in which the event moves back up the object hierarchy To run a function, you create an event listener that detects when a particular event has reached an object in the document object.addEventListener(event, function, capture) New Perspectives on HTML, XHTML, and Dynamic HTML, 4e9

XP Introducing the Event Model Both event models allow you to remove event handlers from objects – The IE event model uses the detachEvent method object.detachEvent (onevent, function) – The W3C event model uses the removeEventListener method object.removeEventListener (event, function, capture) New Perspectives on HTML, XHTML, and Dynamic HTML, 4e10

XP Introducing the Event Model IE Event Model – To attach a function to an object, run: object.attachEvent(onevent, function); where object is the object receiving the event, onevent is the text string of the event handler, and function is the function that runs in response to the event. Multiple functions can be attached to the same event in the same object. – To detach a function, run the following: object.detachEvent(onevent, function); New Perspectives on HTML, XHTML, and Dynamic HTML, 4e11

XP Introducing the Event Model W3C Event Model To run a function when an event reaches an object, use object.addEventListener(event, function, capture); where object is the object receiving the event, event is the text string describing the event, function is the function to run in response to the event, and capture equals true if the event is moving down the document tree and false if the event is bubbling up the tree. To stop listening for an event, run the following: object.removeEventListener(event, function, capture); New Perspectives on HTML, XHTML, and Dynamic HTML, 4e12

XP Introducing the Event Object If the user has pressed a key on the keyboard, you may want to know which key was pressed This type of information is stored in an event object New Perspectives on HTML, XHTML, and Dynamic HTML, 4e13

XP Introducing the Event Object The Internet Explorer Event Object – In the Internet Explorer event model, the event object has the object reference: window.event – If you are dealing with events in the current browser window, you can drop the window reference – One of the more important properties is srcElement – The srcElement property is akin to the “this” keyword New Perspectives on HTML, XHTML, and Dynamic HTML, 4e14

XP Introducing the Event Object New Perspectives on HTML, XHTML, and Dynamic HTML, 4e15

XP Introducing the Event Object The W3C Event Object – In the W3C event model, the event object is inserted as a parameter of whatever function responds to the event – Give the event object any parameter name, but the standard practice is to name the parameter “e” or “evt” – For the DOM event model, the object that initiated an event is returned using the target property New Perspectives on HTML, XHTML, and Dynamic HTML, 4e16

XP Introducing the Event Object New Perspectives on HTML, XHTML, and Dynamic HTML, 4e17

XP Introducing the Event Object Determining the Event Source New Perspectives on HTML, XHTML, and Dynamic HTML, 4e18

XP Working with Mouse Events New Perspectives on HTML, XHTML, and Dynamic HTML, 4e19

XP Working with Mouse Events Keeping Dragged Items on Top New Perspectives on HTML, XHTML, and Dynamic HTML, 4e20

XP Formatting a Drag-and-Drop Action Mouse pointers can be defined using an object’s style properties object.style.cursor=cursorType; Can also define the pointer style in a CSS style declaration cursor: cursorType New Perspectives on HTML, XHTML, and Dynamic HTML, 4e21

XP Formatting a Drag-and-Drop Action New Perspectives on HTML, XHTML, and Dynamic HTML, 4e22

XP Working with Keyboard Events Capturing a Keyboard Event – Three main keyboard events are available to work with keydown: The user presses a key down keypress: Follows immediately after the onkeydown event keyup: The user releases a key New Perspectives on HTML, XHTML, and Dynamic HTML, 4e23

XP Working with Keyboard Events To run a command when the user presses down a key, use the onkeydown event handler To run a command when the user releases a key, use the onkeyup event handler To run a command when the user enters a character from the keyboard, use the onkeypress event handler To retrieve the code of the key pressed by the user during the keydown or keyup event, use the property: – evt.keyCode where evt is the event object. New Perspectives on HTML, XHTML, and Dynamic HTML, 4e24

XP Working with Keyboard Events New Perspectives on HTML, XHTML, and Dynamic HTML, 4e25

XP Working with Keyboard Events Key code values New Perspectives on HTML, XHTML, and Dynamic HTML, 4e26

XP Working with Keyboard Events Modifier Keys – Both event models use the following properties of the event object to determine the state of the Alt, Ctrl, and Shift keys evt.altKey; evt.ctrlKey; evt.shiftKey; – Each of these properties returns a Boolean value indicating whether the modifier key is being pressed – The W3C event model also supports the event object property evt.metaKey; New Perspectives on HTML, XHTML, and Dynamic HTML, 4e27

XP Controlling and Canceling Events JavaScript supports several methods for controlling and canceling events occurring within a browser – event.cancelBubble = value; – evt.stopPropagation() New Perspectives on HTML, XHTML, and Dynamic HTML, 4e28