CSE 154 LECTURE 10: MORE EVENTS

Similar documents
EECS1012. Net-centric Introduction to Computing. Lecture JavaScript Events

Web Programming Step by Step

CSE 154 LECTURE 10: THE DOM TREE

Catching Events. Bok, Jong Soon

Unobtrusive JavaScript. Lecture Outline. CSE 190 M (Web Programming), Spring 2008 University of Washington

Web Programming Step by Step

JavaScript and Events

Hell is other browsers - Sartre. The touch events. Peter-Paul Koch (ppk) DIBI, 28 April 2010

Events: another simple example

Hell is other browsers - Sartre. The touch events. Peter-Paul Koch (ppk) WebExpo, 24 September 2010

JavaScript: Events, the DOM Tree, jquery and Timing

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

729G26 Interaction Programming. Lecture 4

CS193X: Web Programming Fundamentals

CSI 3140 WWW Structures, Techniques and Standards. Browsers and the DOM

JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

Web Design. Lecture 6. Instructor : Cristina Mîndruță Site : Cristina Mindruta - Web Design

Overview. Event Handling. Introduction. Reviewing the load Event. Event mousemove and the event Object. Rollovers with mouseover and mouseout

Events. Mendel Rosenblum. CS142 Lecture Notes - Events

JavaScript Handling Events Page 1

Photo from DOM

Skyway Builder Web Control Guide

Document Object Model (DOM) Level 3 Events

Information Design. Professor Danne Woo! infodesign.dannewoo.com! ARTS 269 Fall 2018 Friday 10:00PM 1:50PM I-Building 212

Chapter 14. Dynamic HTML: Event Model

Web Programming Step by Step

Document Object Model (DOM) Level 2 Events

JSF. JSF and Ajax Basics

of numbers, converting into strings, of objects creating, sorting, scrolling images using, sorting, elements of object

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

SQL AND MORE EVENT VALIDATION

1 The DOM tree CS380

Forms, Form Events, and Validation. Bok, Jong Soon

Touch Forward. Bill Fisher. #touchfwd. Developing Awesome Cross-Browser Touch

skb2 Shaon Barman, Sarah Chasins, Ras Bodik UC Berkeley Sumit Gulwani Microsoft Research

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

Web Programming Step by Step

1. Cascading Style Sheet and JavaScript

CSE 154 LECTURE 8: EVENTS AND TIMERS

j I 8 EVENTS INTERACTI O NS EVENTS TRI GGER CODE RESPONDS CREATE EV ENTS CODE TO USERS

2D1640 Grafik och Interaktionsprogrammering VT Good for working with different kinds of media (images, video clips, sounds, etc.

CITS3403 Agile Web Development Semester 1, 2018

JavaScript: Events, DOM and Attaching Handlers

COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts

JavaScript: Introduction to DOM and Attaching Handlers

Hell is other browsers - Sartre. Ajax Workshop. Peter-Paul Koch (ppk) Fundamentos del Web, 28 October 2008

Document Object Model (DOM) Level 3 Events

B. V. Patel Institute of Business Management, Computer and Information Technology, UTU. B. C. A (3 rd Semester) Teaching Schedule

Chapter 9 Introducing JQuery

HTML and JavaScript: Forms and Validation

Ajax in Practice by Dave Crane Bear Bibeault Jord Sonneveld with Ted Goddard, Chris Gray, Ram Venkataraman and Joe Walker

University of Technology Laser & Optoelectronics Engineering Department Visual basic Lab. LostFocus Resize System event

Fundamentals of Website Development

CERTIFICATE IN WEB PROGRAMMING

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Let's see a couple of examples

I'm Remy. Who uses jquery.

DOM Primer Part 2. Contents

JavaScript: More Syntax and Using Events

JavaScript: A Crash Course

Outline. Lecture 4: Document Object Model (DOM) What is DOM Traversal and Modification Events and Event Handling

HCDE 530: Computational Techniques for HCDE Data Visualization in Web, Part 2

5/19/2015. Objectives. JavaScript, Sixth Edition. Using Touch Events and Pointer Events. Creating a Drag-and Drop Application with Mouse Events

Daniel Riegelhaupt.

Web App Vs Web Site. Some tricks of the trade. Laurent Technical Director, BlackBerry Web Platform

KNOCKOUTJS - EVENT BINDING

title shown on page tab <meta attribute="value"... /> page metadata (h1 for largest to h6 for smallest) emphasis (italic) strong emphasis (bold)

CSE 154 LECTURE 26: JAVASCRIPT FRAMEWORKS

5. Strict mode use strict ; 6. Statement without semicolon, with semicolon 7. Keywords 8. Variables var keyword and global scope variable 9.

Application of Skills: Microsoft PowerPoint 2013 Tutorial

CSC 337. jquery Rick Mercer

Such JavaScript Very Wow

Today s Hall of Fame or Shame example is a feature of Microsoft Office 2007 that gives a preview of what a style command will do to the document

LECTURE-3. Exceptions JS Events. CS3101: Programming Languages: Javascript Ramana Isukapalli

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

SEEM4570 System Design and Implementation Lecture 04 jquery

Interacting with Measurement Studio Graphs in Visual Basic Heather Edwards

CISC 1600 Lecture 2.4 Introduction to JavaScript

CSE 154: Web Programming Autumn 2018

Introduce the basic input devices Input, Interaction and Animation

Exam Questions Demo Microsoft. Exam Questions

NetAdvantage for ASP.NET Release Notes

jquery Tutorial for Beginners: Nothing But the Goods

Web Development & Design Foundations with HTML5

CSE 154: Web Programming Midterm Exam Cheat Sheet HTML. Tags Used in the <head> Section. Tags Used in the <body> Section

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

ENGR/CS 101 CS Session Lecture 4

Lecture 5 Dynamic Documents with JavaScript. Dynamic XHTML

Introduction to Touch events in JavaScript

Using keyboard for input control

Javascript Events. Web Authoring and Design. Benjamin Kenwright

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) What is JavaScript?

LECTURE-2. Functions review HTML Forms. Arrays Exceptions Events. CS3101: Scripting Languages: Javascript Ramana Isukapalli

CSC Web Programming. JavaScript Browser Objects

Web Accessibility Change Your Mouse Pointer Visibility Handbook

FORMS. The Exciting World of Creating RSVPs and Gathering Information with Forms in ClickDimensions. Presented by: John Reamer

Creating Content with iad JS

jquery Basics jquery is a library of JavaScript functions which contains the following functions: HTML Element Selections

PowerTerm WebConnect WebView

Table Basics. The structure of an table

Transcription:

CSE 154 LECTURE 10: MORE EVENTS

Problems with reading/changing styles <button id="clickme">click Me</button> HTML window.onload = function() { document.getelementbyid("clickme").onclick = biggerfont; }; function biggerfont() { var button = document.getelementbyid("clickme"); var size = parseint(button.style.fontsize); button.style.fontsize = (size + 4) + "pt"; } JS output style property lets you set any CSS style for an element problem: you cannot read existing styles with it (you can read ones you set using the DOM.style, but not ones that are set in the CSS file)

Accessing elements' existing styles window.getcomputedstyle(element).propertyname function biggerfont() { // turn text yellow and make it bigger var clickme = document.getelementbyid("clickme"); var size = parseint(window.getcomputedstyle(clickme).fontsize); clickme.style.fontsize = (size + 4) + "pt"; } JS JS output getcomputedstyle method of global window object accesses existing styles

Removing a node from the page function slideclick() { var bullet = document.getelementbyid("removeme"); bullet.parentnode.removechild(bullet); } JS odd idiom: obj.parentnode.remove(obj);

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

Event handler binding window.onload = function() { document.getelementbyid("textbox").onmouseout = booyah; document.getelementbyid("submit").onclick = booyah; // bound to submit button here }; function booyah() { // booyah knows what object it was called on this.value = "booyah"; } JS event handlers attached unobtrusively are bound to the element inside the handler, that element becomes this output

Fixing redundant code with this <input id="huey" type="radio" name="ducks" value="huey" /> Huey <input id="dewey" type="radio" name="ducks" value="dewey" /> Dewey <input id="louie" type="radio" name="ducks" value="louie" /> Louie HTML function processducks() { if (document.getelementbyid("huey").checked) { alert("huey is checked!"); } else if (document.getelementbyid("dewey").checked) { alert("dewey is checked!"); } else { alert("louie is checked!"); } alert(this.value + " is checked!"); } JS if the same function is assigned to multiple elements, each gets its own bound copy output

JavaScript events abort blur change click dblclick error focus keydown keypress keyup load mousedown mousemove mouseout mouseover mouseup reset resize select submit unload the click event (onclick) is just one of many events that can be handled

The event object function name(event) { // an event handler function... } JS Event handlers can accept an optional parameter to represent the event that is occurring. Event objects have the following properties / methods: property name type target timestamp description what kind of event, such as "click" or "mousedown" the element on which the event occurred when the event occurred

Mouse events click dblclick mousedown mouseup user presses/releases mouse button on the element user presses/releases mouse button twice on the element user presses down mouse button on the element user releases mouse button on the element clicking mouseover mouseout mousemove mouse cursor enters the element's box mouse cursor exits the element's box mouse cursor moves around within the element's box movement

Mouse event objects The event passed to a mouse handler has these properties: property/method clientx clienty screenx screeny offsetx offsety button description coordinates in browser window coordinates in screen coordinates in element (non-standard) integer representing which button was pressed (0=Left, 1=Middle, 2=Right)

Mouse event example <pre id="target">move the mouse over me!</pre> window.onload = function() { var target = document.getelementbyid("target"); target.onmousemove = target.onmousedown = showcoords; }; HTML function showcoords(event) { document.getelementbyid("target").innerhtml = + "screen : (" + event.screenx + ", " + event.screeny + ")\n" + "client : (" + event.clientx + ", " + event.clienty + ")\n" + "button : " + event.button; } JS screen : (333, 782) client : (222, 460) button : 0 output

Keyboard/text events name focus blur keydown keyup keypress select description this element gains keyboard focus (attention of user's keyboard) this element loses keyboard focus user presses a key while this element has keyboard focus user releases a key while this element has keyboard focus user presses and releases a key while this element has keyboard focus this element's text is selected or deselected

Key event objects property name keycode altkey, ctrlkey, shiftkey description ASCII integer value of key that was pressed (convert to char with String.fromCharCode) true if Alt/Ctrl/Shift key is being held issue: if the event you attach your listener to doesn't have the focus, you won't hear the event possible solution: attach key listener to entire page body, document, an outer element, etc.

Key event example document.getelementbyid("textbox").onkeydown = textkeydown;... function textkeydown(event) { var key = String.fromCharCode(event.keyCode); if (key == 's' && event.altkey) { alert("save the document!"); this.value = this.value.split("").join("-"); } } JS each time you push down any key, even a modifier such as Alt or Ctrl, the keydown event fires if you hold down the key, the keydown event fires repeatedly keypress event is a bit flakier and inconsistent across browsers

Some useful key codes keyboard key event keycode Backspace 8 Tab 9 Enter 13 Escape 27 Page Up, Page Down, End, Home 33, 34, 35, 36 Left, Up, Right, Down 37, 38, 39, 40 Insert, Delete 45, 46 Windows/Command 91 F1 - F12 112-123

Page/window events name contextmenu error load, unload resize scroll unload description the user right-clicks to pop up a context menu an error occurs when loading a document or an image the browser loads the page the browser window is resized the user scrolls the viewable part of the page up/down/left/right the browser exits/leaves the page The above can be handled on the window object

Multiple listeners to the same event element.addeventlistener("event", function); var button = document.getelementbyid("mybutton"); button.addeventlistener("click", func1); // button.onclick = func1; button.addeventlistener("click", func2); // button.onclick = func2; JS JS if you assign onclick twice, the second one replaces the first addeventlistener allows multiple listeners to be called for the same event (note that you do not include "on" in the event name!)

Multiple window.onload listeners window.onload = function; window.addeventlistener("load", function); JS it is considered bad form to directly assign to window.onload multiple.js files could be linked to the same page, and if they all need to run code when the page loads, their window.onload statements will override each other by calling window.addeventlistener instead, all of them can run their code when the page is loaded

Stopping an event event method name preventdefault stoppropagation description stops the browser from doing its normal action on an event; for example, stops the browser from following a link when <a> tag is clicked, or stops browser from submitting a form when submit button is clicked stops the browser from showing this event to any other objects that may be listening for it you can also return false; from your event handler to stop an event

Stopping an event, example <form id="exampleform" action="http://foo.com/foo.php">...</form> window.onload = function() { var form = document.getelementbyid("exampleform"); form.onsubmit = checkdata; }; function checkdata(event) { if (document.getelementbyid("state").length!= 2) { alert("error, invalid city/state."); // show error message event.preventdefault(); return false; // stop form submission } } JS