Events: another simple example

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

JAVASCRIPT BASICS. Handling Events In JavaScript. In programing, event-driven programming could be a programming

Fundamentals of Website Development

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

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

CISH-6510 Web Application Design and Development. Overview of JavaScript. Overview

Introduction to DHTML

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

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

DOM Primer Part 2. Contents

Want to add cool effects like rollovers and pop-up windows?

JavaScript Handling Events Page 1

1$ 5 ! $ 6 4 * Source: 0 "!*! 0! * 8 97 ?!$ 5 0 *! 4! $ 0 : * ' () 7;7 7<7

HTML User Interface Controls. Interactive HTML user interfaces. Document Object Model (DOM)

Catching Events. Bok, Jong Soon

CITS3403 Agile Web Development Semester 1, 2018

Beijing , China. Keywords: Web system, XSS vulnerability, Filtering mechanisms, Vulnerability scanning.

Key features. Nothing to do with java It is the Client-side scripting language Designed to add interactivity to HTML pages

JSF - H:INPUTSECRET. Class name of a validator that s created and attached to a component

Chapter 14 - Dynamic HTML: Event Model

Photo from DOM

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

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

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

link document.getelementbyid("coffee").style.borderwidth = "0px" document.getelementbyid("tea").style.borderwidth = "10px"

CSS The web browser uses its own resources, and eases the burden on the server. It has fewer features than server side scripting.

Installation and Configuration Manual

Place User-Defined Functions in the HEAD Section

TEXTAREA NN 2 IE 3 DOM 1

Javascript. A key was pressed OR released. A key was released. A mouse button was pressed.

JavaScript is described in detail in many books on the subject, and there is excellent tutorial material at

Name Related Elements Type Default Depr. DTD Comment

Introduction to JavaScript, Part 2

Chapter 14. Dynamic HTML: Event Model

Dynamic Web Pages - Integrating JavaScript into a SAS Web Application Caroline Bahler, ASG, Inc.

JSF - H:SELECTONEMENU

Continues the Technical Activities Originated in the WAP Forum

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

UNIT - III. Every element in a document tree refers to a Node object. Some nodes of the tree are

CSC Javascript

JS Tutorial 3: InnerHTML Note: this part is in last week s tutorial as well, but will be included in this week s lab

JavaScript: Events, the DOM Tree, jquery and Timing

JSF - H:SELECTONERADIO

New Media Production Lecture 7 Javascript

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

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

The first sample. What is JavaScript?

By the end of this section of the practical, the students should be able to:

Introduction to JavaScript

CSE 154 LECTURE 10: MORE EVENTS

Canvas & Brush Reference. Source: stock.xchng, Maarten Uilenbroek

Digitizing Sound and Images III Storing Bits

JavaScript: A Crash Course

Document Object Model (DOM) Level 3 Events

Note: Java and JavaScript are two completely different languages in both concept and design!

EVENT-DRIVEN PROGRAMMING

Q1. What is JavaScript?

CISC 1600 Lecture 2.4 Introduction to JavaScript

Document Object Model (DOM) Level 2 Events

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 15: JavaScript loops, Objects, Events - Spring 2011

Full file at Tutorial 2: Working with Operators and Expressions

Client vs Server Scripting

Web Development & Design Foundations with HTML5

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

MooTools 1.2. Jacob Gube Garrick Cheung. Chapter No. 5 "Working with Events"

a.) All main headings should be italicized. h1 {font-style: italic;} Use an ordinary selector. HTML will need no alteration.

Lecture 14. Introduction to JavaScript. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

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

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

Experience the Magic of On-the-fly Modernization. Screen Customization Guide. for Genie Version 3.0

JAVASCRIPT FOR PROGRAMMERS

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

XHTML Mobile Profile. Candidate Version Feb Open Mobile Alliance OMA-TS-XHTMLMP-V1_ C

Introduction to using HTML to design webpages

Document Object Model (DOM) Level 3 Events

Web Development & Design Foundations with HTML5, 8 th Edition Instructor Materials Chapter 14 Test Bank

Session 6. JavaScript Part 1. Reading

CE212 Web Application Programming Part 2

Lesson 5: Introduction to Events

Skyway Builder Web Control Guide

Web Design and Application Development

Dynamic HTML becomes HTML5. HTML Forms and Server Processing. Form Submission to Web Server. DHTML - Mouse Events. CMST385: Slide Set 8: Forms

Client-side Techniques. Client-side Techniques HTML/XHTML. Static web pages, Interactive web page: without depending on interaction with a server

CITS1231 Web Technologies

3Lesson 3: Functions, Methods and Events in JavaScript Objectives

Session 16. JavaScript Part 1. Reading

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

JSF - H:PANELGRID. JSF Tag. Rendered Output. Tag Attributes. The h:panel tag renders an HTML "table" element. Attribute & Description.

JavaScript Programming Chris Seddon

Appendix A. XHTML 1.1 Module Reference

JavaScript II CSCI 311 SPRING 2017

CICS 515 a Internet Programming Week 3. Mike Feeley

This tutorial has been designed for beginners in HTML5 to make them understand the basicto-advanced

IS 242 Web Application Development I

Automated Article Links Identification for Web-based Online Medical Journals

Interactor Tree. Edith Law & Mike Terry

Web GUI Development CSE5544

Events. Mendel Rosenblum. CS142 Lecture Notes - Events

Dynamic documents with JavaScript

Transcription:

Internet t Software Technologies Dynamic HTML part two IMCNE A.A. 2008/09 Gabriele Cecchetti Events: another simple example Every element on a web page has certain events which can trigger JavaScript functions. For example, we can use the onclick event of a button element to indicate that a function will run when a user clicks on the button: <html><head><title>event Handlers</title> <script> function ciao() { window.alert( You clicked button!"); </script></head> <body> <button onclick="ciao()"> Click Here! </button> </body></html> 2

Examples of actions which can trigger JavaScript functions A mouse click A web page or an image loading Mousing over a hot spot on the web page Selecting an input box in an HTML form Submitting an HTML form A keystroke Note: the function will not be executed before the event occurs! G. Cecchetti Internet Software Technologies 3 Main event handlers (1/2) Event handler onabort onfocus Description abort loading an <img> acquire window focus or form s element focus onblur loss form s element focus onchange change <input> and <textarea> content) onclick click on form s elements or on link ondblclick double click on same elements onkeydown Key down onkeyup Release a key onkeypress Press and hold a key onload page loaded onunload page unloaded 4

Main event handlers (2/2) Event handler onmousemove onmouseout onmouseover onmousedown onmouseup onmove onresize onsubmit Description cursor moving cursor going out from map, link or area cursor over map, link or area mouse button pressed and cursor over a link mouse button released and cursor over a link window moving window resizing form submitting submit button pressed onreset form resetting reset button pressed onselect selecting content on a page 5 Mouse and Keyboards attributes Property altkey button clientx clienty ctrlkey metakey relatedtarget screenx screeny shiftkey Description Returns whether or not the "ALT" key was pressed when an event was Returns which mouse button was clicked when an event was Returns the horizontal coordinate of the mouse pointer when an event was Returns the vertical coordinate of the mouse pointer when an event was Returns whether or not the "CTRL" key was pressed when an event was Returns whether or not the "meta" key was pressed when an event was Returns the element related to the element that the event Returns the horizontal coordinate of the mouse pointer when an event was Returns the vertical coordinate of the mouse pointer when an event was Returns whether or not the "SHIFT" key was pressed when an event was 6

Other event attributes Property Description bubbles Returns a Boolean value that indicates whether or not an event is a bubbling event cancelable Returns a Boolean value that indicates whether or not an event can have its default action prevented currenttarget Returns the element whose event listeners the event eventphase Returns which h phase of the event flow is currently being evaluated target Returns the element that the event timestamp Returns the time stamp, in milliseconds, from the epoch (system start or event trigger) type Returns the name of the event G. Cecchetti Internet Software Technologies 7 Example: what is the unicode of the key pressed? <html><head> function whichbutton(event) { alert(event.keycode); </script></head> <body onkeyup="whichbutton(event)"> <p>press a key on your keyboard.<br> An alert box will alert the unicode of the key pressed.</p> </body></html> G. Cecchetti Internet Software Technologies 8

Example: what are the coordinates of the cursor? <html><head> function show_coords(event) { x=event.clientx; y=event.clienty; alert("x coords: " + x + ", Y coords: " + y); </script> </head> <body onmousedown="show_coords(event)"> <p>click in the document. An alert box will alert the x and y coordinates of the mouse pointer.</p> </body></html> G. Cecchetti Internet Software Technologies 9 Example: which mouse button was clicked? <html><head> function whichbutton(event) { if (event.button==2) else alert("you clicked the right mouse button!"); alert("you clicked the left mouse button!"); </script></head> <body onmousedown="whichbutton(event)"> <p>click in the document. An alert box will alert which mouse button you clicked.</p> </body></html> G. Cecchetti Internet Software Technologies 10

Adding events via scripting This is an alternative technique to classic event handler. You can assign and set up event handlers to elements using scripting, and inside your script. This allows for the event handlers to be dynamically set up, without having to mess around with ihthe HTML codes on the page. When setting up event handlers for an element directly inside your script, the code to execute for the events must be defined inside a function. G. Cecchetti Internet Software Technologies 11 Example of event handler via scripting <a id="test" href=http://retis.sssup.it> retis.sssup.it</a> " function changestatus(){ window.status="click here for RETIS site" return true function changebackstatus() { window.status= status='' document.getelementbyid("test").onmouseover=changestatus document.getelementbyid("test").onmouseout=changebackstatus </script> Notice how we attached the two functions to execute for the two events- the function names without the parenthesis. This is called a reference call to the function.\when assigning a function to an event via scripting, always use a function call. If you were to include the parenthesis of the function inside the definition, an error will be generated. G. Cecchetti Internet Software Technologies 12

The DOM event flow The DOM introduces a new concept for detecting events and assigning corresponding event handlers to react to them. Naturally, it also supports the 2 conventional techniques discussed earlier. G. Cecchetti Internet Software Technologies 13 How events are handled in the DOM? The DOM interprets all user action very differently from the user. Example: moving the mouse over a link: to the DOM the following events took place 1) the mouse was moved over the document 2) the mouse was moved over any tags containing the target <a> tag 3) the mouse was moved over the <a> tag of the link in question 4) the mouse was moved over any tags containing the target <a> tag 5) the mouse was moved over the document. Summary: Mouse over document Mouse over any containment tags of <A> Mouse over destination <A> Mouse over any containment tags of <A> Mouse over document We call the event as it travels to the intended element event capture, and as it travels back event bubble. G. Cecchetti Internet Software Technologies 14

Event capture and Event bubble Event capture it basically refers to the process of an event as it travels to its destination element. Actually, it further refers to the ability to "capture", or intercept this event. Event bubble it is the exact opposite of event capture, and points to the traveling upwards of an event from the source element to the topmost, or document. G. Cecchetti Internet Software Technologies 15 Assigning event handlers in the DOM (1/2) In light of the new event flow (event capture and event bubble) that occurs inside the DOM, new methods for assigning event handlers are introduced. A concept for attaching event handlers is introduced- event listeners. Two methods: object.addeventlistener(event, function, capture) object.removeeventlistener(event, function, capture) where the first method assigns an event handler, while the second allows you to then remove it. G. Cecchetti Internet Software Technologies 16

Assigning event handlers in the DOM (2/2) object.addeventlistener(event, (, function,, capture) ) object.removeeventlistener(event, function, capture) where: event should contain the event you wish to detect, such as click (onclick minus the "on" prefix) function should contain the reference call to the function to execute for this event, such as dothis (dothis() () minus the parenthesis) capture should contain the Boolean value "true" or "false"; a value of "true"" causes function to be executed when the event is detected at the capture phase. a value of "false" causes function to be executed when event is at bubble phase. G. Cecchetti Internet Software Technologies 17 Example of using addeventlistener <div id="test"> Some text over div </div> function alertit(){ alert("you moved your mouse over me!") document.getelementbyid( getelementbyid("test") test ).addeventlistener( addeventlistener("mouse over",alertit,false) </script> Moving your mouse over the DIV now will cause an alert message to popup. p p G. Cecchetti Internet Software Technologies 18

Multiple addeventlistener() It can be used multiple times to attach multiple functions to the same event for the same element. For example, we can have 3 separate onmouseover events for the previous DIV example: document.getelementbyid("test").addeventlistener("mouse over",alertthis,false) l document.getelementbyid("test").addeventlistener("mouse over",alertdat,false), document.getelementbyid("test").addeventlistener("mouse over",alerttidat,false) And all three will be responded to onmouseover. Note that two scripts can both apply the same event handler to an element without t the former being cancelled out. G. Cecchetti Internet Software Technologies 19 References to event specification Look for them in Internet. Pay attention to the different browser implementations. G. Cecchetti Internet Software Technologies 20

HTML DOM style specification The Style object represents an individual style statement. The Style object can be accessed from the document or from the elements to which that style is applied. Syntax: document.getelementbyid("id").style.property="value" The property name usually is the style property as defined in the CSS specification. G. Cecchetti Internet Software Technologies 21 Example: changing the case letter on the fly <html> <head> function changetext() { document.getelementbyid("p1").style.texttransform="ca pitalize"; </script> </head> <body> <p id="p1">this is an example paragraph.</p> <input type="button" onclick="changetext()" value="convert first letter of each word" /> </body></html> G. Cecchetti Internet Software Technologies 22