Jozef Goetz contribution, 2015 1 ©1992-2014 by Pearson Education, Inc. All Rights Reserved.

Slides:



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

Session 11 Dynamic HTML: Event Model and Filters Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
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.
20-Jun-15 JavaScript and HTML Simple Event Handling.
Page 1 of 39 Javascript Chapters 13, 14 Vadim Parizher Computer Science Department California State University, Northridge Fall 2003 Slides from text Book.
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Events.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Chapter 19: Adding JavaScript
Chapter 5 © 2003 by Addison-Wesley, Inc. 1 Chapter 5 JavaScript and HTML Documents.
Chapter 5 JavaScript and HTML Documents. © 2006 Pearson Addison-Wesley. All rights reserved JavaScript Execution Environment - The JavaScript.
JavaScript Part 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
1 JavaScript: Event Model November 1, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
1 Dynamic HTML III: Event Model Introduction Event model –Scripts respond to user actions and change page accordingly Moving mouse Scrolling screen.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Dynamic HTML: Event Model Outline 16.1Introduction 16.2Event ONCLICK 16.3Event ONLOAD.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 14 - Dynamic HTML: Event Model Outline 14.1 Introduction 14.2 Event onclick 14.3 Event onload 14.4.
Pemrograman Teknologi Internet W06: Functions and Events.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Dynamic HTML: Event Model Outline 14.1 Introduction 14.2 Event onclick 14.3 Event onload.
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
Chapter 14: Dynamic HTML: Event Model Presented by: Colbie Brown CS340 Java Web Development Dr. Gloria Carter Love.
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.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
JavaScript, Fourth Edition
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Dynamic HTML: Event Model Outline 16.1Introduction 16.2Event ONCLICK 16.3Event ONLOAD.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 14 - Dynamic HTML: Event Model Outline 14.1 Introduction 14.2 Event onclick 14.3 Event onload 14.4.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 4 Dynamic HTML & CSS.
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.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
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.
H.Melikyan/4910/031 Programming the World Wide Web JavaScript Dr.Hayk Melikyan Departmen of Mathematics and CS JavaScript and HTML Documents.
Host Objects: Browsers and the DOM
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Dynamic HTML: Event Model Outline 14.1 Introduction 14.2 Event onclick 14.3 Event onload.
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.
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.
Web Development & Design Foundations with HTML5
Introduction to JavaScript Events
Chapter 14: DHTML: Event Model
Web Development & Design Foundations with HTML5 7th Edition
JAVASCRIPTS AND HTML DOCUMENTS
14 A Brief Look at JavaScript and jQuery.
Chapter 14 - Dynamic HTML: Event Model
13 JavaScript: Events.
JavaScript and Ajax (JavaScript Events)
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
Presentation transcript:

Jozef Goetz contribution, © by Pearson Education, Inc. All Rights Reserved.

Jozef Goetz contribution, The user should feel in control of the computer; not the other way around. This is achieved in applications that embody three qualities: responsiveness, permissiveness, and consistency.  Inside Macintosh, Volume 1 Apple Computer, Inc., 1985 We are responsible for actions performed in response to circumstances for which we are not responsible.  Allan Massie

Jozef Goetz contribution, OBJECTIVES In this chapter you will learn:  The concepts of  events,  event handlers and  event bubbling.  To create and register event handlers that respond to mouse and keyboard events.  To use the event object to get information about an event.  To recognize and respond to many common events.

Jozef Goetz contribution, Introduction 13.2 Reviewing Event onload 13.3 Event onmousemove, the event Object, and this 13.4 Rollovers with onmouseover and onmouseout 13.5 Form Processing with onfocus and onblur 13.6 More Form Processing with onsubmit and onreset 13.7 Event Bubbling 13.8 More Events 13.9 Web Resources

Jozef Goetz contribution,  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events and event handling  help make web applications  more responsive,  dynamic and  interactive 13.1 Introduction

Jozef Goetz contribution,  Functions that handle events  Assigning an event handler (a function) to an event on a DOM (Document Object Model) node is called registering an event handler  The DOM gives you access to all the elements on a web page.  Using JavaScript, you can create, modify and remove elements in the page dynamically.  With the DOM, HTML elements can be treated as objects, and many attributes of HTML elements can be treated as properties of those objects.  Then, objects can be scripted (through their id attributes) with JavaScript to achieve dynamic effects. Registering Event Handlers 27 var traditional1 = document.getElementById( "traditional" ); 28 traditional1.onclick = handleEvent;

Jozef Goetz contribution,  3 models for registering event handlers  Inline model treats events as attributes of HTML elements  Traditional model assigns the name of the function to the event property of a DOM node  1. In the inline model, the value of the HTML attribute is a JavaScript statement to be executed when the event occurs  2. In the traditional model, the value of the event property of a DOM node is the name of a function to be called when the event occurs  Traditional registration of event handlers allows easy assignment of event handlers to many elements using repetition statements (see lines 25-29), instead of adding an inline event handler to each HTML element  3. W3C standard event-registration model (a modification of a traditional one) – next slide Registering Event Handlers 27 var traditional1 = document.getElementById( "traditional" ); 28 traditional1.onclick = handleEvent;

Jozef Goetz contribution, Create the my.html file with a link to my.css: my Count = 0 2. Create my.css file 3.Create my.js. Register start() on load: window.addEventListener( "load", start, false ); 4.Define start(): get by ID placeholder representations of HTML elements and register all needed functions for specific events. For example: function start() { count = document.getElementById( "count" ); document.getElementById( "incrementButton" ).addEventListener( "click", increment, false ); } 5.Define global (instance) variables and define functions to execute when the user triggers any event registered before. For example: var counter = 0; var count; function increment() {++counter; count.innerHTML = counter; } // end function increment Steps to create a W3C standard event-registration model for well structured JavaScript Website

Jozef Goetz contribution, registering.html Function to handle the onclick event Registers the event handler using the traditional model by setting onclick property. Assign the function (as data) handleEvent to div’s onclick property – no parentheses Delegate mechanism in C#. Traditional registration allows quick and easy assignment of event handlers to many elements

Jozef Goetz contribution, Outline registering.html (2 of 2) Registers the event handler using the inline model and assign its return value to the onclick property

Jozef Goetz contribution, Common Programming Error  Putting quotes around the function name when registering it using the traditional model would assign a string to the onclick property of the node - a string cannot be called.  28 traditional1.onclick = functionName; or  window.addEventListener( "load", functionName, false ); Putting parentheses after the function name when registering it would call the function immediately and assign its return value to the onclick property.

Jozef Goetz contribution, Outline Fig.13.2 onload.html (1 of 2) Calls function updateTime every second (1000 ms) Updates the timer display in the soFar element of the document If a script from called in the head attempts to get a DOM node for an HTML element in the body, getElementById returns null because the body has not yet loaded. Avoid this by putting your script in a function using the onload event to call the function.

Jozef Goetz contribution, onload.html (2 of 2) As soon as the body has loaded, startTimer is called

Jozef Goetz contribution,  onload event fires whenever an element finishes loading successfully  If a script in the head attempts to get a DOM node for an HTML element in the body, getElementById returns null because the body has not yet loaded  Trying to get an element in a page before the page has loaded is a common error.  Avoid this by putting your script in a function using the onload event to call the function Reviewing Event onload

Jozef Goetz contribution,  onmousemove event fires whenever the user moves the mouse  event object stores information about the event that called the event-handling function  ctrlKey property contains a boolean which reflects whether the Ctrl key was pressed during the event  shiftKey property reflects whether the Shift key was pressed during the event  In an event-handling function, this refers to the DOM object on which the event occurred  this keyword enables one event handler to apply a change to one of many DOM elements, depending on which one received the event 13.3 Event onmousemove, the event Object and this

Jozef Goetz contribution,  Internet Explorer and Firefox do not implement the same event models  Firefox and other W3C-compliant browsers (e.g., Safari, Opera) pass the event object “e” as an argument to the event-handling function  Internet Explorer, on the other hand, stores the event object in the event property of the window object 13.3 Event onmousemove, the event Object and this

Jozef Goetz contribution, Outline Fig.13.3 draw.html (1 of 5) Sets the dimensions of a table of cells that will act as a canvas Eliminates space between table cells Creates table of cells for the canvas

Jozef Goetz contribution, 2015 Gets the event object in IE. Internet Explorer stores the event object in the event property of the window object 18 Outline draw.html (2 of 5) Assigns processMouseMove as the event handler for the cell’s onmousemove event It fires and calls processMouseMove whenever the user moves the mouse over the element Gets the event object in Firefox. W3C-compliant browsers (e.g., Safari, Opera) pass the event object “e” as an argument. e object stores info about the event that called the function Determines which key is pressed and colors the cell accordingly this refers to the cell (the DOM object) that received the event

Jozef Goetz contribution, Outline draw.html (3 of 5)

Jozef Goetz contribution,

Jozef Goetz contribution, Common Programming Error  Although you can omit the tbody element in an HTML table, without it you cannot append tr elements as children of a table using JavaScript.  While Firefox treats appended rows as members of the table body,  Internet Explorer will not render any table cells that are dynamically added to a table outside a thead, tbody or tfoot element.

Jozef Goetz contribution, Fig | Some event object properties.

Jozef Goetz contribution,  When the mouse cursor enters an element, an onmouseover event occurs for that element  When the mouse cursor leaves the element, an onmouseout event occurs for that element  Creating an Image object and setting its src property preloads the image  The event object e stores the node on which the action occurred  In Internet Explorer, this node is stored in the event object’s srcElement property: target = e.srcElement - line 30, 68, 71  In Firefox, it is stored in the event object’s target property: target = e.target - - line 71 below 13.4 Rollovers with onmouseover and onmouseout

Jozef Goetz contribution, Outline Fig.13.5 Onmouseoverout.html (1 of 8) Preloads the heading images, nodes are stored in the event object’s srcElement property Preloading images used in rollover effects prevents a delay the first time an image is displayed. It works with IE

Jozef Goetz contribution, Outline Onmouseoverout.html (2 of 8) Stores the return value of getTarget to variable target - we can’t use “ this” because we have not defined an event handler for each element in the document Changes the heading’s image to image2 If target has a defined id (true of table cells and the heading), changes its color to that id, e.g. color =#0000FF

Jozef Goetz contribution, Outline Onmouseoverout.html (3 of 8) Replaces image2 with image1 If the element’s id is defined, makes the displayed text equal to the id Returns the targeted node in Internet Explorer stored in e.srcElement and Firefox stored in e.target Registers the onmouseover and onmouseout events in the document object by assigning functions mouseOver and mouseOut

Jozef Goetz contribution, Outline Onmouseoverout.html (4 of 8)

Jozef Goetz contribution, Outline Onmouseoverout.html (5 of 8)

Jozef Goetz contribution,

Jozef Goetz contribution,

Jozef Goetz contribution,  onfocus event fires when an element gains focus  i.e., when the user clicks a form field or uses the Tab key to move between form elements  onblur fires when an element loses focus  i.e., when another control gains the focus 13.5 Form Processing with onfocus and onblur

Jozef Goetz contribution, Outline Fig onfocusblur.html (1 of 2) Array of help messages

Jozef Goetz contribution, onfocusblur.html (2 of 2) Displays the corresponding help message in the div element at the bottom of the document When a user clicks into a field, the onfocus event is fired, which feeds the appropriate message number to function helpText in order to display the help message When an element loses focus, the onblur event is fired, and helpText(6) is called, clearing the old message from the screen div element where the help message is displayed

Jozef Goetz contribution,  onsubmit and onreset events fire when a form is submitted or reset, respectively  Anonymous function  A function that is defined with no name  Created in nearly the same way as any other function, but with no identifier after the keyword function  Useful when creating a function for the sole purpose of assigning it to an event handler  confirm method asks the users a question, presenting them with an OK button and a Cancel button  If the user clicks OK, confirm returns true ; otherwise, confirm returns false  By returning either true or false, event handlers dictate whether the default action for the event is taken  If an event handler returns true or does not return a value, the default action is taken once the event handler finishes executing 13.6 More Form Processing with onsubmit and onreset

Jozef Goetz contribution, Fig.13.7 Onsubmitreset.html (1 of 3)

Jozef Goetz contribution, Onsubmit reset.html (2 of 3) Creates an anonymous function (with no name after “function”) to register as an event handler for the onsubmit and onreset event. Uses confirm method of the window object to return a boolean stating whether or not the form should be submitted or reset. If the user click OK, confirm returns true and the corresponding action will take place, otherwise if the user click Cancel no action will take place.

Jozef Goetz contribution, Outline Onsubmitreset.html (3 of 3)

Jozef Goetz contribution, Form Processing with onfocus and onblur

Jozef Goetz contribution,  Event bubbling  The process whereby events fired in child elements “bubble” up to their parent elements  When an event is fired on an element, it is first delivered to the element’s event handler (if any), then to the parent element’s event handler (if any)  If you intend to handle an event in a child element alone, you should cancel the bubbling of the event in the child element’s event-handling code by using the cancelBubble property of the event object 13.7 Event Bubbling

Jozef Goetz contribution, Outline Fig.13_0bubbling.html (1 of 3) e. ancelBublle property cancels = false, so the bubbling of the event in the child element’s event handling code. So it will bubble up to the document – see function documentClick()

Jozef Goetz contribution, bubbling.html (2 of 3) Cancels event bubbling, So it will not bubble up to the document Registers events for clicking in the two p elements, which are children of the document object Registers an event for the document object. It trigers when the onclick even bubles up to the document Common Programming Error 13.5 Forgetting to cancel event bubbling when necessary may cause unexpected results in your scripts.

Jozef Goetz contribution, Event Bubbling

Jozef Goetz contribution,  The following slide contains a list of some events supported by both Firefox and Internet Explorer 13.8 More Events Fig | Cross-browser events. (Part 1 of 2.)

Jozef Goetz contribution, Fig | Cross-browser events. (Part 1 of 2.)

Jozef Goetz contribution, More Events  The actual DOM event names begin with " on ", but we show the names you use with addEventListener here.