Modern JavaScript Develop And Design Instructor’s Notes Chapter 8 – Event Handling Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman.

Slides:



Advertisements
Similar presentations
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,
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.
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.
20-Jun-15 JavaScript and HTML Simple Event Handling.
Ajax Dr Jim Briggs WEBP Ajax1. 2 Ajax Asynchronous JavaScript And XML Method of creating more interactive web applications Moves more of the application.
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.
JavaScript Client Side scripting. Client-side Scripts Client-side scripts, which run on the user’s workstation can be used to: Validate user inputs entered.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 2- JavaScript in Action Modern JavaScript Design And Develop Copyright © 2012 by Larry.
CP476 Internet Computing JavaScript and HTML1 1.JavaScript Execution Environment The JavaScript Window object represents the window in which the browser.
JavaScript and The Document Object Model MMIS 656 Web Design Technologies Acknowledgements: 1.Notes from David Shrader, NSU GSCIS 2.Some material adapted.
Chapter 6 JavaScript and AJAX. Objectives Explain the purpose and history of JavaScript Describe JavaScript features Explain the event-driven nature of.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
Modern JavaScript Develop And Design Instructor’s Notes Chapter 10 – Working with Forms Modern JavaScript Design And Develop Copyright © 2012 by Larry.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
Lecture Set 13 Drawing Mouse and Keyboard Events Part B – Mouse and Keyboard Events.
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.
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.
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.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
MIS333k(Poynor) Chapter 2. Intro to VBA Events Where to plug in your programs?
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
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
CISC 110 Day 6 Introduction to Events. Outline Event-Driven Programming Event Classes Hierarchy –Event Class –Mouse Events –Keyboard Events Registering.
CHAPTER 7 JQUERY WHAT IS JQUERY? jQuery is a script. It is written in JavaScript.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
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.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
Overview  The Document Object Model (DOM) is an API that allows programs to interact with HTML (or XML) documents In typical browsers, the JavaScript.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
CHAPTER 13 FORM ENHANCEMENT & VALIDATION
JavaScript and HTML Simple Event Handling 11-May-18.
Introduction to JavaScript Events
Modern JavaScript Develop And Design
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and HTML Simple Event Handling 19-Sep-18.
CSE 154 Lecture 11: More Events.
Working with the Event Model
Events.
Introduction to jQuery
Lecture 11: Keyboard Events
CSE 337 Lecture 10: Events.
JavaScript and Events CS Programming Languages for Web Applications
CSE 337 Lecture 10: Events.
5.1 JavaScript Execution Environment
CS7026 jQuery Events.
Lecture 11: Keyboard 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:

Modern JavaScript Develop And Design Instructor’s Notes Chapter 8 – Event Handling Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman

Objectives Create event handlers using the traditional approach Create event handlers using the W3C approach Create event handlers for older versions of IE Recognize bad, outdated ways of creating event handlers

More Objectives Define a function to reliably create event handlers for all browsers Identify the most common events and event types Reference the event itself in JavaScript code Access useful event properties

More Objectives Know what key was touched or character was typed Prevent the default browser behavior for an event Comprehend event phases Delegate event handling

The Premise window.onload = init; document.getElementById('theForm').onsubmit = process;

Creating Event Handlers inline traditional W3C (DOM Level 2) IE

Inline Event Listeners Some Link

Traditional Event Handlers window.onload = init; window.onload = function() { // Do whatever. } if (typeof window.onload == 'function') { // Exists! window.onload = null;

W3C Event Handling window.addEventListener('load', process, false); window.addEventListener('load', calculate, false); object.addEventListener('event', functionName, false); window.removeEventListener('load', process, false);

IE Event Handling window.attachEvent('onload', process); object.attachEvent('onevent', functionName); window.detachEvent('onload', process);

Reliable Event Handling function addEvent(obj, type, fn) { if (obj && obj.addEventListener) { // W3C obj.addEventListener(type, fn, false); } else if (obj && obj.attachEvent) { // Older IE obj.attachEvent('on' + type, fn); } addEvent(window, 'load', init); var theForm = document.getElementById('theForm'); addEvent(theForm, 'submit', process);

Event Types Input Device Keyboard Browser Form Touch

Input Device Buttons click mousedown mouseup dblclick contextmenu

Input Device Movements mouseout mouseover mousemove

Keyboard Events keydown keyup keypress

Counting Characters function limitText() { var comments = document.getElementById('comments'); var count = comments.value.length; document.getElementById('count').value = count; if (count > 100) { comments.value = comments.value.slice(0,100); } } // End of limitText() function. window.onload = function() { addEvent(document.getElementById('comments')('comments'), 'keyup', limitText); };

Browser Events load unload resize scroll copy cut paste

Form Events focus blur reset change select

Event Accessibility Keyboard focus blur Input Device mouseover mouseout Some Text // JavaScript: addEvent(document.getElementById('link'), 'mouseover', doSomething); addEvent(document.getElementById('link'), 'focus', doSomething);

Referencing the Event function someEventHandler(e) { if (typeof e == 'undefined') e = window.event; // Use e. }

Event Properties target/srcElement type function reportEvent(e) { if (typeof e == 'undefined') e = window.event; var target = e.target || e.srcElement; var msg = target.nodeName + ': ' + e.type + '\n'; document.getElementById('output').value += msg; } // End of reportEvent() function.

Key Press var charCode = (typeof e.which == 'number') ? e.which : e.keyCode; String.fromCharCode(charCode);

Prevent Default Behavior if (typeof e == 'undefined') e = window.event; if (e.preventDefault) { e.preventDafault(); } else { e.returnValue = false; } return false;

Event Phases This is a Title This is a paragraph. This is a link.

Delegating Event Handling window.onload = function() { var theForm = document.getElementById('theForm'); theForm.onchange = validateForm; };