Events CS380 1. The keyword this  all JavaScript code actually runs inside of an object  by default, code runs inside the global window object  all.

Slides:



Advertisements
Similar presentations
Chapter 08: Adding Adding Interactivity Interactivity With With Behaviors Behaviors By Bill Bennett Associate Professor MSJC CIS MVC.
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.
DOM and timers CS Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code.
The Web Warrior Guide to Web Design Technologies
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
DHTML - Introduction Introduction to DHTML, the DOM, JS review.
CMPT241 Web Programming Intro to JavaScript. Project 4.
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
DOM and timers CS Problems with JavaScript JavaScript is a powerful language, but it has many flaws:  the DOM can be clunky to use  the same code.
Unobtrusive JavaScript
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
CSE 154 LECTURE 19: EVENTS AND TIMERS. Anonymous functions function(parameters) { statements; } JS JavaScript allows you to declare anonymous functions.
More Events and Validation CS Page/window events CS380 2.
CSCI 6962: Server-side Design and Programming Introduction to Active Server Pages.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
CSE 154 LECTURE 18: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
USING XML AS A DATA SOURCE. Data binding is a process by which information in a data source is stored as an object in computer memory. In this presentation,
CSE 154 Lecture 20: The DoM tree.
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.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
Event-Driven Programming Chapter Page Section: Section or division of an HTML page Generic block element Example: What's the difference between.
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.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
XPages Example: Generating Dynamic Editable Fields from a Document Collection Author: John Mackey Groupware Solutions Inc.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
4. Events are where it happens! Bear Bibeault Yehuda Katz.
Host Objects: Browsers and the DOM
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Introduction 1.HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document. 2.Events are normally used in combination.
CSE 154 LECTURE 7: THE DOCUMENT OBJECT MODEL (DOM); UNOBTRUSIVE JAVASCRIPT.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.
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.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
11 jQuery Web Service Client. 22 Objectives You will be able to Use JSON (JavaScript Object Notation) for communcations between browser and server methods.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Lecture 7: The Document Object Model (DOM); Unobtrusive JavaScript
© 2015, Mike Murach & Associates, Inc.
Introduction to JavaScript Events
DHTML Javascript Internet Technology.
CSE 154 Lecture 11: More Events.
Event Driven Programming & User Defined Functions
DHTML Javascript Internet Technology.
Events.
Functions, Regular expressions and Events
CSE 337 Lecture 10: Events.
Introduction to DHTML, the DOM, JS review
Recognizing JavaScript Features
CSE 337 Lecture 10: Events.
JavaScript Basics Topics Review Important Methods Writing Functions
CSc 337 Lecture 9: The DoM tree.
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
JavaScript Event Handling For Interaction and Casual Games
Presentation transcript:

Events CS380 1

The keyword this  all JavaScript code actually runs inside of an object  by default, code runs inside the global window object  all global variables and functions you declare become part of window  the this keyword refers to the current object CS380 2 this.fieldName // access field this.fieldName = value; // modify field this.methodName(parameters); // call method JS

Event handler binding  event handlers attached unobtrusively are bound to the element  inside the handler, that element becomes this (rather than the window) CS380 3 function pageLoad() { $("ok").onclick = okayClick; // bound to okButton here } function okayClick() { // okayClick knows what DOM object this.innerHTML = "booyah"; // it was called on } window.onload = pageLoad; JS

Fixing redundant code with this CS380 4 function processDucks() { if ($("huey").checked) { alert("Huey is checked!"); } else if ($("dewey").checked) { alert("Dewey is checked!"); } else { alert("Louie is checked!"); } alert(this.value + " is checked!"); } JS Huey Dewey Louie HTML

More about events 5  the click event (onclick) is just one of many events that can be handled  problem: events are tricky and have incompatibilities across browsers  reasons: fuzzy W3C event specs; IE disobeying web standards; etc.  solution: Prototype includes many event-related features and fixes

Attaching event handlers the Prototype way  to use Prototype's event features, you must attach the handler using the DOM element  object's observe method (added by Prototype)  pass the event of interest and the function to use as the handler  handlers must be attached this way for Prototype's event features to work 6 element.onevent = function; element.observe("event", "function"); JS // call the playNewGame function when the Play button is clicked $("play").observe("click", playNewGame); JS

Attaching multiple event handlers with $$  you can use $$ and other DOM walking methods to unobtrusively attach event handlers to  a group of related elements in your window.onload code 7 // listen to clicks on all buttons with class "control" that // are directly inside the section with ID "game" window.onload = function() { var gameButtons = $$("#game > button.control"); for (var i = 0; i < gameButtons.length; i++) { gameButtons[i].observe("click", gameButtonClick); } }; function gameButtonClick() {... } JS

The Event object  Event handlers can accept an optional parameter to represent the event that is occurring. Event objects have the following properties / methods: 8 function name(event) { // an event handler function... } JS

Mouse events CS380 9

Mouse events CS380 10

Mouse event objects CS380 11

Mouse event objects CS * replaces non-standard properties pageX and pageY ** replaces non-standard properties button and which

The Event object 13 window.onload = function() { $("target").observe("mousemove", showCoords); }; function showCoords(event) { this.innerHTML = "pointer: (" + event.pointerX() + ", " + event.pointerY() + ")\n" + "screen : (" + event.screenX + ", " + event.screenY + ")\n" + "client : (" + event.clientX + ", " + event.clientY + ")"; JS Move the mouse over me! HTML CS380