Introduction to JavaScript Events Instructor: Sergey Goldman 1.

Slides:



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

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.
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.
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.
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 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.
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.
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.
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.
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.
 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.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
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.
Dialog boxes in JavaScript Events in JavaScript – What are they – “Which events are there?” – “How do I register event handlers to an HTML element?” –
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.
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.
JavaScript Challenges Answers for challenges
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
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.
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
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
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.
Jozef Goetz contribution, © by Pearson Education, Inc. All Rights Reserved.
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.
JavaScript and HTML Simple Event Handling 11-May-18.
Introduction to JavaScript Events
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and HTML Simple Event Handling 19-Sep-18.
Chapter 14 - Dynamic HTML: Event Model
13 JavaScript: Events.
Events Comp 205 Fall 2017.
Events.
JavaScript and Events CS Programming Languages for Web Applications
JavaScript and Ajax (JavaScript Events)
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
Web Programming and Design
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:

Introduction to JavaScript Events Instructor: Sergey Goldman 1

Main Idea Do not write event - The events are already happening. –Page loaded, mouse clicked, moved etc. – events are generated –These events are built into JavaScript. onload onclick onmousemove onblur Onfocus You write what's called the event handler (the event listener). 2

1 way. Function to handle/listen Not so good –script mixed with HTML –a lot of ; for multiple statements –not reusable code –if JavaScript disables 3

2 way. Anonymous function The name of the element, then a dot, then the name of the event. window.onload means the onload event of the window object; nameField.onblur means the onblur event of the nameField object; When they click a DOM element, we want to do something, so we use the equal sign don't have to give it a name because we're saying exactly when this gets executed, which is when they click myelement. 4

3 way. Using event Listener click instead of onclick mouseover instead onmouseover comma, the function run (callback) then false (advanced event handling) Can have multiple handlers –document.addEventListener(‘click’, func1, false); –document.addEventListener(‘click’, func2, false); < IE9 attachEvent(‘onclick’, myFunction); 5

Cross-browser OR use jQuery or else No () 6

Ex.: click anywhere Hello World myscript.js document.onclick = function() { alert(“I clicked”); }; 7

 The window object’s load event fires when the window finishes loading successfully (i.e., all its children are loaded and all external files referenced by the page are loaded)  Every DOM element has a load event, but it’s most commonly used on the window object.  Ex:  The load event’s handler creates an interval timer that updates a span with the number of seconds that have elapsed since the document was loaded.  The document’s paragraph contains the span Reviewing the load Event 8

9

10

 An event handler is a function that responds to an event.  Assigning an event handler to an event on a DOM node is called registering an event handler  Method addEventListener can be called multiple times on a DOM node to register more than one event-handling method for an event.  It’s also possible to remove an event listener by calling removeEventListener with the same arguments that you passed to addEventListener to register the event handler.  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 13.2 Reviewing the load Event (Cont.) 11

 Two 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  The inline model places calls to JavaScript functions directly in HTML code.  The following code indicates that JavaScript function start should be called when the body element loads: 13.2 Reviewing the load Event (Cont.) (one more time) 12

 The traditional model uses a property of an object to specify an event handler.  The following JavaScript code indicates that function start should be called when document loads: document.onload = "start()"; 13.2 Reviewing the load Event (Cont.) (one more time) 13

mousemove event occurs whenever the user moves the mouse over the web page Ex: –creates a simple drawing program that allows the user to draw inside a table element in red or blue by holding down the Shift key or Ctrl key and moving the mouse over the box. –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 13.3 Event mouseMove and the event Object 14

15

16

17

18

19

20

When the mouse cursor enters an element, an mouseover event occurs for that element When the mouse cursor leaves the element, a mouseout event occurs for that element Creating an Image object and setting its src property preloads the image 13.4 Rollovers with mouseover and mouseout 21

22

23

24

25

26

27

28

29

30

focus 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 blur fires when an element loses focus –i.e., when another control gains the focus 13.5 Form Processing with focus and blur 31

32

33

34

35

36

submit and reset events fire when a form is submitted or reset, respectively The anonymous function executes in response to the user’s submitting the form by clicking the Submit button or pressing the Enter key. 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 submit and reset 37

38

39

40

Event bubbling –The process whereby events fired on 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 41

42

43

44

45

46

47

The following slide lists some common events and their descriptions. The actual DOM event names begin with " on ", but we show the names you use with addEventListener here More Events 48

49

50