Introduction to JavaScript Events

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.
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.
 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.
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.
JavaScript Part 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
1 Dynamic HTML III: Event Model Introduction Event model –Scripts respond to user actions and change page accordingly Moving mouse Scrolling screen.
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.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
 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.
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.
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.
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.
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.
Tarik Booker CS 120 California State University, Los Angeles.
5.1 JavaScript Execution Environment
JavaScript and HTML Simple Event Handling 11-May-18.
Web Development & Design Foundations with HTML5
JavaScript (JS) Basics
Chapter 14: DHTML: Event Model
JavaScript Functions.
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and Events.
14 A Brief Look at JavaScript and jQuery.
JavaScript and HTML Simple Event Handling 19-Sep-18.
Chapter 14 - Dynamic HTML: Event Model
13 JavaScript: Events.
CSE 154 Lecture 11: More Events.
JavaScript and HTML Documents
Events Comp 205 Fall 2017.
CHAPTER 7 JavaScripts & HTML Documents
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

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).

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

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.

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);

Cross-browser OR use jQuery or else No ()

Ex.: click anywhere <html> <head> </head> <body> <h1>Hello World</h1> <script src="myscript.js"></script> </body> </html> myscript.js document.onclick = function() { alert(“I clicked”); };

13.2 Reviewing the load Event 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.

13.2 Reviewing the load Event (Cont.) 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.) (one more time) 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: <body onload = "start()"> 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.3 Event mouseMove and the event Object 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.4 Rollovers with mouseover and mouseout 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.5 Form Processing with focus and blur 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.6 More Form Processing with submit and reset 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.7 Event Bubbling 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.8 More Events 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.