XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.

Slides:



Advertisements
Similar presentations
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.
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.
The Web Warrior Guide to Web Design Technologies
Tutorial 16 Working with Dynamic Content and Styles.
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.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
JavaScript, Third Edition
JavaScript, Fourth Edition
Tutorial 13 Working with Objects and Styles. XP Objectives Learn about objects and the document object model Reference documents objects by ID, name,
Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 10.
 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.
XP Tutorial 5 Buttons, Behaviors, and Sounds. XP New Perspectives on Macromedia Flash MX Buttons Interactive means that the user has some level.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
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.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
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.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
XP Tutorial 12 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Special Effects Creating Rollovers, Menus, Filters, and Transitions.
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.
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.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
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.
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.
Host Objects: Browsers and the DOM
XP Tutorial 5 New Perspectives on JavaScript, Comprehensive1 Working with Special Effects Creating Rollovers, Menus, Filters, and Transitions.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 6. Dynamic Documents With JavaScript.
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.
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)
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.
Excel Tutorial 8 Developing an Excel Application
Introduction to JavaScript Events
JavaScript and Events.
13 JavaScript: Events.
CSE 154 Lecture 11: More Events.
Working with the Event Model
WEB PROGRAMMING JavaScript.
Working with the Event Model
Tutorial 6 Creating Dynamic Pages
Events.
Working with Special Effects
JavaScript and Events CS Programming Languages for Web Applications
CS7026 jQuery Events.
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
Presentation transcript:

XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart

XP Tutorial 8New Perspectives on JavaScript, Comprehensive2 Objectives Learn different methods for applying event handlers Study event propagation in the Internet Explorer event model Understand event propagation in the DOM event model Create functions that resolve the differences between the event models

XP Tutorial 8New Perspectives on JavaScript, Comprehensive3 Objectives Work with the properties of the event object under both models Identify the object that initiated an event Determine the coordinates of a mouse event Create objects that can be dragged and dropped

XP Tutorial 8New Perspectives on JavaScript, Comprehensive4 Objectives Work with cursor styles Create functions that respond to double-click events Determine which mouse button a user clicked Work with keyboard events, including determining which key a user pressed Understand how to work with modifier keys

XP Tutorial 8New Perspectives on JavaScript, Comprehensive5 Working with Events

XP Tutorial 8New Perspectives on JavaScript, Comprehensive6 Working with Events

XP Tutorial 8New Perspectives on JavaScript, Comprehensive7 Working with Events

XP Tutorial 8New Perspectives on JavaScript, Comprehensive8 Working with Events Event Handlers –One common way of responding to an event is by adding an event handler attribute to an element’s tag...

XP Tutorial 8New Perspectives on JavaScript, Comprehensive9 Working with Events Event Handlers

XP Tutorial 8New Perspectives on JavaScript, Comprehensive10 Working with Events Event Handlers as Object Properties –Another way to apply an event handler is to treat it as an object property object.onevent = function –For example, to run the moveItem() function whenever the mousemove event occurs within the item1 element, you could run the following JavaScript command: document.getElementById("item1").onmousemove =moveItem;

XP Tutorial 8New Perspectives on JavaScript, Comprehensive11 Working with Events Event Handlers as Object Properties –One of the main disadvantages of handling events as object properties is the inability to include a function’s parameter values in a property –In addition, you can assign only one function at a time to a particular object and event

XP Tutorial 8New Perspectives on JavaScript, Comprehensive12 Working with Events Event Handlers as Script Elements –Can also invoke event handlers as attributes of the script element

XP Tutorial 8New Perspectives on JavaScript, Comprehensive13 Working with Events Using an Event Handler to Cancel an Action –Can cancel the default action for any event by assigning a function to the event handler that returns the Boolean value false document.links[0].onclick=disableLink; function disableLink() { return false; }

XP Tutorial 8New Perspectives on JavaScript, Comprehensive14 Working with Events Assigning an Event Handler

XP Tutorial 8New Perspectives on JavaScript, Comprehensive15 Introducing the Internet Explorer Event Model The way that a browser works with events is called its event model Consider two event models in this tutorial: the Internet Explorer event model developed for the Internet Explorer browser, and the DOM event model developed by the W3C for DOM Level 2 The Internet Explorer event model is supported by Internet Explorer version 5 and higher The DOM event model is supported by Netscape 6 and 7, Firefox, and other Mozilla browsers. A third event model was developed for Netscape 4 and Netscape 4.7

XP Tutorial 8New Perspectives on JavaScript, Comprehensive16 Introducing the Internet Explorer Event Model Event Bubbling –In the Internet Explorer Event model, events are initiated at the bottom of the document tree and rise to the top of the object hierarchy in a process known as event bubbling

XP Tutorial 8New Perspectives on JavaScript, Comprehensive17 Introducing the Internet Explorer Event Model Canceling Event Bubbling –In some scripts you may want to prevent an event from propagating up the document tree –To prevent event bubbling from occurring, run the following command when the event reaches the level at which you want the propagation to stop event.cancelBubble=true; –To turn event bubbling back on, run the command event.cancelBubble = false;

XP Tutorial 8New Perspectives on JavaScript, Comprehensive18 Introducing the Internet Explorer Event Model Canceling Event Bubbling –To cancel an event at the current level as well as all levels above the object, run the command event.returnValue = false;

XP Tutorial 8New Perspectives on JavaScript, Comprehensive19 Introducing the Internet Explorer Event Model Attaching and Detaching Events –The Internet Explorer event model overcomes this limitation with the attachEvent() method object.attachEvent(onevent, function) –To detach one of these functions from the mouseover event, you would use the detachEvent() method

XP Tutorial 8New Perspectives on JavaScript, Comprehensive20 Introducing the DOM Event Model In the DOM model, an event starts at the top and moves down the object hierarchy until it reaches the target of the event; at that point, the event bubbles back up the object hierarchy

XP Tutorial 8New Perspectives on JavaScript, Comprehensive21 Introducing the DOM Event Model

XP Tutorial 8New Perspectives on JavaScript, Comprehensive22 Introducing the DOM Event Model In the DOM 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)

XP Tutorial 8New Perspectives on JavaScript, Comprehensive23 Introducing the DOM Event Model Can prevent an event from propagating through the object hierarchy using the method evt.stopPropagation() To cancel an event entirely, use the method evt.preventDefault()

XP Tutorial 8New Perspectives on JavaScript, Comprehensive24 Creating a Cross-Browser Event Model Since two event models exist, with different approaches and syntax, you need to determine which event model a user’s browser supports and then run commands appropriate for that model

XP Tutorial 8New Perspectives on JavaScript, Comprehensive25 Creating a Cross-Browser Event Model

XP Tutorial 8New Perspectives on JavaScript, Comprehensive26 Working with Event Objects 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

XP Tutorial 8New Perspectives on JavaScript, Comprehensive27 Working with Event Objects The Internet Explorer Event Object –In the Internet Explorer event model, the event object has the object reference windowObject.event –If you are dealing with events in the current browser window, you can drop the windowObject reference –One of the more important properties is srcElement –The srcElement property is akin to the “this” keyword

XP Tutorial 8New Perspectives on JavaScript, Comprehensive28 Working with Event Objects The Internet Explorer Event Object

XP Tutorial 8New Perspectives on JavaScript, Comprehensive29 Working with Event Objects The DOM Event Object –In the DOM 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

XP Tutorial 8New Perspectives on JavaScript, Comprehensive30 Working with Event Objects The DOM Event Object

XP Tutorial 8New Perspectives on JavaScript, Comprehensive31 Creating the grabIt() Function Function has to perform the following tasks –Identify the object that lies beneath the pointer –Determine the page coordinates of the mouse pointer at the moment the user depresses the mouse button –Calculate the difference between the coordinates of the mouse pointer and the coordinates of the selected object –Assign functions to the object that run whenever the user moves the mouse pointer or releases the mouse button

XP Tutorial 8New Perspectives on JavaScript, Comprehensive32 Creating the grabIt() Function Determining the Event Source

XP Tutorial 8New Perspectives on JavaScript, Comprehensive33 Creating the grabIt() Function Determining the Event Coordinates

XP Tutorial 8New Perspectives on JavaScript, Comprehensive34 Creating the grabIt() Function Determining the Event Coordinates

XP Tutorial 8New Perspectives on JavaScript, Comprehensive35 Creating the grabIt() Function Calculating the Distance from the Pointer

XP Tutorial 8New Perspectives on JavaScript, Comprehensive36 Creating the grabIt() Function Calculating the Distance from the Pointer

XP Tutorial 8New Perspectives on JavaScript, Comprehensive37 Creating the moveIt() Function The moveIt() function needs to perform the following tasks –Determine the current location of the mouse pointer –Maintain dragItem at a constant distance from the mouse pointer

XP Tutorial 8New Perspectives on JavaScript, Comprehensive38 Creating the moveIt() Function

XP Tutorial 8New Perspectives on JavaScript, Comprehensive39 Creating the moveIt() Function

XP Tutorial 8New Perspectives on JavaScript, Comprehensive40 Creating the dropIt() Function

XP Tutorial 8New Perspectives on JavaScript, Comprehensive41 Redefining the Drag-and-Drop Feature Keeping Dragged Items on Top

XP Tutorial 8New Perspectives on JavaScript, Comprehensive42 Redefining the Drag-and-Drop Feature Returning a Dragged Item to Its Starting Point

XP Tutorial 8New Perspectives on JavaScript, Comprehensive43 Redefining the Drag-and-Drop Feature Returning a Dragged Item to Its Starting Point

XP Tutorial 8New Perspectives on JavaScript, Comprehensive44 Redefining the Drag-and-Drop Feature Canceling the selectStart Event

XP Tutorial 8New Perspectives on JavaScript, Comprehensive45 Formatting a Dragged Object 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

XP Tutorial 8New Perspectives on JavaScript, Comprehensive46 Formatting a Dragged Object

XP Tutorial 8New Perspectives on JavaScript, Comprehensive47 Formatting a Dragged Object Changing the Color

XP Tutorial 8New Perspectives on JavaScript, Comprehensive48 Working with the Double-Click Event

XP Tutorial 8New Perspectives on JavaScript, Comprehensive49 Working with the Double-Click Event

XP Tutorial 8New Perspectives on JavaScript, Comprehensive50 Working with the Mouse Button In the DOM event model, the button values are 0=left, 1=middle, 2=right In the Internet Explorer event model, these values are 0=left, 2=right, 4=middle For Netscape 6 the values are 1=left, 2=middle, 3=right Right and middle mouse buttons usually have default actions May not wish to interfere with these default actions

XP Tutorial 8New Perspectives on JavaScript, Comprehensive51 Working with Keyboard Events Capturing a Keyboard Event –Three main keyboard events are available to work with keydown: The user has pressed a key down keyup: The user has released a key keypress: The user has pressed and released a key

XP Tutorial 8New Perspectives on JavaScript, Comprehensive52 Working with Keyboard Events Examining Key Codes

XP Tutorial 8New Perspectives on JavaScript, Comprehensive53 Working with Keyboard Events Creating the KeyDrag() function

XP Tutorial 8New Perspectives on JavaScript, Comprehensive54 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 altKey; ctrlKey; shiftKey; –Each of these properties returns a Boolean value indicating whether the modifier key is being pressed –The DOM event model also supports the event object property metaKey;

XP Tutorial 8New Perspectives on JavaScript, Comprehensive55 Tips for Working with Events Create customized cross-browser functions that resolve the differences among the event models Position any objects that will be dragged and dropped using inline styles prior to running the drag-and-drop commands In a drag-and-drop application, provide users with additional visual feedback that an object is being moved, such as color changes and pointer changes Capture keyboard and mouse events to make your Web page more accessible to users with disabilities