Events DOM Event Model. HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

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

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
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.
WEAVING CODE EXTENSIONS INTO JAVASCRIPT Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington, Microsoft Research.
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.
CP476 Internet Computing JavaScript and HTML1 1.JavaScript Execution Environment The JavaScript Window object represents the window in which the browser.
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.
JavaScript and The Document Object Model MMIS 656 Web Design Technologies Acknowledgements: 1.Notes from David Shrader, NSU GSCIS 2.Some material adapted.
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.
Unobtrusive JavaScript
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.
© 2000 – All Rights Reserved - Page 1 Introduction to JavaScript Programming Part Two.
CS346 Javascript-7A Events1 DOM Document Object Model and Events.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
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
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
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 –
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
Thursday, August 6 th, 2015 Instructor: Craig Duckett Event Handling.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
4. Events are where it happens! Bear Bibeault Yehuda Katz.
1 Javascript CS , Spring What is Javascript ? Browser scripting language  Dynamic page creation  Interactive  Embedded into HTML pages.
H.Melikyan/4910/031 Programming the World Wide Web JavaScript Dr.Hayk Melikyan Departmen of Mathematics and CS JavaScript and HTML Documents.
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
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.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
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.
Third lecture Event 27/2/2016 JavaScript Tutorial.
5.1 JavaScript Execution Environment
JavaScript and HTML Simple Event Handling 11-May-18.
Introduction to JavaScript Events
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and Events.
JavaScript and HTML Simple Event Handling 19-Sep-18.
CSE 154 Lecture 11: More Events.
Event Driven Programming & User Defined Functions
CHAPTER 7 JavaScripts & HTML Documents
Events.
Introduction to jQuery
JavaScript and Events CS Programming Languages for Web Applications
JavaScript and Ajax (JavaScript 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:

Events DOM Event Model

HTML Events "on" + event name attribute value is javascript javascript runs 1st return false to STOP browser event (if any)

Layered Events click me What happens? Why don't you try it? click me Why doesn't the return false stop it? ONLY stops the from going to href=""

DOM Events DOM API's event model - separate from HTML SAME BASIC EVENT NAMES AND BEHAVIORS no more "on" prefix! many MORE events! create your own events! Programs somewhat like JAVA's SWING API More flexible and powerful (except in IE6)

Event Triggered When clickclicked once (like :active) mouseinThe mouse moves over an element mouseoutThe mouse moves off an element mouseoverlike :hover mousemoverepeated event as fast cpu mouseupbutton up - use this one mousedownbutton down scrollwhen scrollbars move keydownkey pressed down keyupkey was let up - use this one

Event Triggered When blurElement becomes inactive selectA user selects a field in a form focusElement becomes active (anything tab-able) changeThe value of an element changes submitA user submits a form resetA form resets resizewindow resize loadA document or image loads unloadA document unloads abortThe loading of an image is interrupted errorloading a document or image goes bad

ElementEvent click, mouseover, mouseout about, error, load mouseover, mouseout blur, error, focus, load, unload blur, error, focus, load, unload blur, focus submit, reset blur, focus, change, select click click click blur, focus, change blur, focus, change, select blur, focus, change OL D

addEventListener() HTMLElement.addEventListener( "event name without on prefix. ex: click", function object which is.call(this, event), optional: true - capture event early );

HTML is easier. Why use DOM? HTML on"event" requires CHANGES TO HTML Automation, productivity, bandwidth Flexibility getElementsByClassName() code reuse! - programming costs more More features; + invalid HTML events can exist in DOM (some events only exist in DOM)

function hi(){ alert("hello!"); } document.body.addEventListener( 'load', hi ); // after page is loads // an alert window says "hello!"

removeEventListener() dispatchEvent() fake events; trigger made up events requires Event object: e= document.createEvent("name") e.initEvent(...) - setup properties see also...

HTML tags have a hierarchy parent tags can overlap events paragraph is parent child tag Try it. experiment. capture / bubble

body p a a Click! Capture (true) Bubble (false) 's onclick="runs" 's onclick="runs" 's href is followed 's href is followed

function capture(){ alert("captured!"); } function bubble(){ alert("bubbled"); } tagObject= document.getElementById('id'); tagObject.addEventListener( 'click', bubble, false ); tagObject.addEventListener( 'click', capture, true ); // A click on tagObject: // 1 st says "hello!" (capture phase) // 2 nd says "bye" (bubble phase - default) // 3 rd tagObject does whatever it normally does //- likely nothing unless it is,, etc.

Demo

Common Use Bubble only - HTML onEvents are bubble Usually Bubble is all you want MSIE never supported capture Compatibility libraries forced to do same So, addEventListener('event', function); false is redundant, undefined comes out false

Event object Describes details of the event; timestamp, keys click HTML onEvents set var event for you You never work with Event, you use event an instance of Event

tagObject.addEventListener('click', your_f, false) what it does: your_f.call( tagObject, event) function your_f(e){ alert(this); alert(e); } FYI: Function.call(this, parameters); your_f.call( this=object, parameters,... ); event use in DOM

Useful event methods.preventDefault() stops HTML tags from seeing event function your_f(e){ e.preventDefault(); } similar to HTML's.stopPropagation() event stops traveling down/up to other tags

Useful event Properties.target = same as this; the object of the event.keyCode, charCode = character pressed.shiftKey,.ctrlKey,.altKey = true/false if down.button = mouse button # down (only 1 at a time).clientX,.clientY,.screenX,.screenY = mouse.bubbles = event is bubbling up

variable "binding" Problem: event handler function lack parameters onclick="your_f(anything, I, want...)" addEventListener( 'click', your_f) nothing you want can be given to your_f

Solution 1 Javascript can hack any object tagObject.addEventListener('click', your_f); tagObject.myInformation= "hello"; function your_f(e) { alert(this.myInformation); alert(e.target.myInformation); }

Solution: exploit Javascript's memory management function f(){ var x=5; setTimeout( function(){alert(x);},1000); } f(); variables actually DIE when nothing uses them Solution 2: variable "binding"

variable "binding" (see Closure) Variables are references to memory storage memory storage is freed when there are no more references If you ADD references to the storage it will live until all of them are gone javascript event handling HEAVILY uses "binding"

function attach_an_event(){ var myInformation= "hello"; var your_f= function(e){ alert(myInformation); } tagObject.addEventListener('click', your_f); } // myInformation is "bound" to your_f // it lives until your_f dies and that will not die // if you removedEventListener then it would die // if you removed tagObject then it would die

function your_f(your_evt, your_info){ alert(this);alert(your_evt.type);alert(your_info); } function bind( tag, evt, f, info ){ var middleman= function(event){ return f.call(event.target, event, info); }; tag.addEventListener(evt, middleman); } bind(document.body, "click", your_f, "hello");