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

Similar documents
Digital Electronics A Practical Approach with VHDL William Kleitz Ninth Edition

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

Programmable Logic Controllers. Second Edition

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

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

Pearson New International Edition. The 8051 Microcontroller and Embedded Systems Mazidi Mazidi McKinlay Second Edition

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

GO! with Microsoft Access 2010 Introductory Gaskin Mclellan Graviett First Edition

Elementary Number Theory. Kenneth H. Rosen Sixth Edition

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

Events: another simple example

Fundamentals of Website Development

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

Engineering with Excel. Ronald W. Larsen Fourth Edition

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

Technical Communication Today

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

Introduction to DHTML

Workplace Communications The Basics George J. Searles Fifth Edition

JavaScript Handling Events Page 1

Advanced Electronic Communications Systems Wayne Tomasi Sixth Edition

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

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

CITS3403 Agile Web Development Semester 1, 2018

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

Photo from DOM

Numerical Analysis Timothy Sauer Second Edition

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

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

JavaScript and Events

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

DOM Primer Part 2. Contents

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

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

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

Calculus & Its Applications Larry J. Goldstein David Lay Nakhle I. Asmar David I. Schneider Thirteenth Edition

Catching Events. Bok, Jong Soon

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

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

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

Installation and Configuration Manual

TEXTAREA NN 2 IE 3 DOM 1

CSC Javascript

Engineering Problem Solving with C++

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

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

Skyway Builder Web Control Guide

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

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

Continues the Technical Activities Originated in the WAP Forum

Place User-Defined Functions in the HEAD Section

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

Apprenticeships BUSINESS & Functional Skills Level 1 ICT

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

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

GLOBAL EDITION. Elementary Surveying. An Introduction to Geomatics FOURTEENTH EDITION. Charles D. Ghilani Paul R. Wolf

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

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

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

Apprenticeships. Functional Skills Level 2 ICT

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

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

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

GLOBAL EDITION. Assembly Language. for x86 Processors SEVENTH EDITION. Kip R. Irvine

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

Q1. What is JavaScript?

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

JSF - H:SELECTONEMENU

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

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

JSF - H:SELECTONERADIO

Such JavaScript Very Wow

Apprenticeships CUSTOMER. Functional Skills Level 1 MATHEMATICS

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

Introduction to using HTML to design webpages

About the Author. Liability

Name Related Elements Type Default Depr. DTD Comment

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

DC71 INTERNET APPLICATIONS JUNE 2013

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

Editorial Director, ECS: Publishing Administrator, International Edition: Editor-in-Chief:

Web Development & Design Foundations with HTML5

Introduction to 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

EVENT-DRIVEN PROGRAMMING

JSF. JSF and Ajax Basics

Javascript Lecture 23

Chapter 14 - Dynamic HTML: Event Model

For Tricia: never dull, never boring, the smartest and bravest person I know

The first sample. What is JavaScript?

CITS1231 Web Technologies

CSE 154 LECTURE 10: MORE EVENTS

Introduction to JavaScript, Part 2

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

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

Heinemann is a registered trademark of Pearson Education Limited

JAVASCRIPT FOR PROGRAMMERS

1. Cascading Style Sheet and JavaScript

Session 6. JavaScript Part 1. Reading

New Media Production Lecture 7 Javascript

Transcription:

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsoned.co.uk Pearson Education Limited 2014 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without either the prior written permission of the publisher or a licence permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6 10 Kirby Street, London EC1N 8TS. All trademarks used herein are the property of their respective owners. The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners. ISBN 10: 1-292-02431-3 ISBN 13: 978-1-292-02431-8 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library Printed in the United States of America

which the buttons appear. To access the arrays, the DOM address of the form object must first be obtained, as in the following example: <form id = "vehiclegroup"> <input type = "checkbox" name = "vehicles" value = "car" /> Car <input type = "checkbox" name = "vehicles" value = "truck" /> Truck <input type = "checkbox" name = "vehicles" value = "bike" /> Bike </form> The implicit array, vehicles, has three elements, which reference the three objects associated with the three checkbox elements in the group. This array provides a convenient way to search the list of checkboxes in a group. The checked property of a checkbox object is set to true if the button is checked. For the preceding sample checkbox group, the following code counts the number of checkboxes that were checked: var numchecked = 0; var dom = document.getelementbyid("vehiclegroup"); for (index = 0; index < dom.vehicles.length; index++) if (dom.vehicles[index].checked) numchecked++; Radio buttons can be addressed and handled exactly as are the checkboxes in the foregoing code. 4 Events and Event Handling The HTML 4.0 standard provided the first specification of an event model for markup documents. This model is sometimes referred to as the DOM 0 event model. Although the DOM 0 event model is limited in scope, it is the only event model supported by all browsers that support JavaScript. A complete and comprehensive event model was specified by DOM 2. The DOM 2 model is supported by the FX3 and Chrome browsers. However, although IE9 supports it, earlier versions of IE do not. Our discussion of events and event handling is divided into two parts, one for the DOM 0 model and one for the DOM 2 model. 4.1 Basic Concepts of Event Handling One important use of JavaScript for Web programming is to detect certain activities of the browser and the browser user and provide computation when those activities occur. These computations are specified with a special form of programming called event-driven programming. In conventional (non-event-driven) 203

programming, the code itself specifies the order in which it is executed, although the order is usually affected by the program s input data. In event-driven programming, parts of the program are executed at completely unpredictable times, often triggered by user interactions with the program that is executing. An event is a notification that something specific has occurred, either in the browser, such as the completion of the loading of a document, or a browser user action, such as a mouse click on a form button. Strictly speaking, an event is an object that is implicitly created by the browser and the JavaScript system in response to something having happened. An event handler is a script that is implicitly executed in response to the appearance of an event. Event handlers enable a Web document to be responsive to browser and user activities. One of the most common uses of event handlers is to check for simple errors and omissions in user input to the elements of a form, either when they are changed or when the form is submitted. This kind of checking saves the time of sending incorrect form data to the server. If you are familiar with the exceptions and exception-handling capabilities of a programming language such as C++ or Java, you should see the close relationship between events and exceptions. Events and exceptions occur at unpredictable times, and both often require some special program actions. Because events are JavaScript objects, their names are case sensitive. The names of all event objects have only lowercase letters. For example, click is an event, but Click is not. Events are created by activities associated with specific HTML elements. For example, the click event can be caused by the browser user clicking a radio button or the link of an anchor tag, among other things. Thus, an event s name is only part of the information pertinent to handling the event. In most cases, the specific HTML element that caused the event is also needed. The process of connecting an event handler to an event is called registration. There are two distinct approaches to event handler registration, one that assigns tag attributes and one that assigns handler addresses to object properties. These are further discussed in Sections 5 and 6. The write method of document should never be used in an event handler. Remember that a document is displayed as its markup is parsed by the browser. Events usually occur after the whole document is displayed. If write appears in an event handler, the content produced by it might be placed over the top of the currently displayed document. The remainder of this section and Sections 5 through 7 describe the DOM 0 event model and some of its uses. 4.2 Events, Attributes, and Tags HTML4 defined a collection of events that browsers implement and with which JavaScript can deal. These events are associated with HTML tag attributes, which can be used to connect the events to handlers. The attributes have names that are closely related to their associated events. Table 1 lists the most commonly used events and their associated tag attributes. 204

Table 1 Events and their tag attributes Events blur change click dblclick focus keydown keypress keyup load mousedown mousemove mouseout mouseover mouseup reset select submit unload Tag Attribute onblur onchange onclick ondblclick onfocus onkeydown onkeypress onkeyup onload onmousedown onmousemove onmouseout onmouseover onmouseup onreset onselect onsubmit onunload In many cases, the same attribute can appear in several different tags. The circumstances under which an event is created are related to a tag and an attribute, and they can be different for the same attribute when it appears in different tags. An HTML element is said to get focus when the user puts the mouse cursor over it and clicks the left mouse button. An element can also get focus when the user tabs to the element. When a text element has focus, any keyboard input goes into that element. Obviously, only one text element can have focus at one time. An element becomes blurred when the user moves the cursor away from the element and clicks the left mouse button or when the user tabs away from the element. An element obviously becomes blurred when another element gets focus. Several nontext elements can also have focus, but the condition is less useful in those cases. 205

Table 2 shows (1) the most commonly used attributes related to events, (2) tags that can include the attributes, and (3) the circumstances under which the associated events are created. Only a few of the situations shown in the table are discussed in this chapter. Table 2 Event attributes and their tags Attributes Tag Description onblur <a> The link loses focus. <button> <input> <select> The button loses focus. The input element loses focus. The text area loses focus. The selection element loses focus. onchange <input> The input element is changed and loses focus. <select> The text area is changed and loses focus. The selection element is changed and loses focus. onclick <a> The user clicks the link. <input> The input element is clicked. ondblclick Most elements The user double-clicks the left mouse button. onfocus <a> The link acquires focus. <input> <select> The input element acquires focus. A text area acquires focus. A selection element acquires focus. onkeydown <body>, form elements A key is pressed. onkeypress <body>, form elements A key is pressed and released. onkeyup <body>, form elements A key is released. onload <body> The document is finished loading. onmousedown Most elements The user clicks the left mouse button. onmousemove Most elements The user moves the mouse cursor within the element. onmouseout Most elements The mouse cursor is moved away from being over the element. 206

Table 2 Event attributes and their tags (continued) Attributes Tag Description onmouseover Most elements The mouse cursor is moved over the element. onmouseup Most elements The left mouse button is unclicked. onreset <form> The reset button is clicked. onselect <input> Any text in the content of the element is selected. Any text in the content of the element is selected. onsubmit <form> The Submit button is pressed. onunload <body> The user exits the document. As mentioned previously, there are two ways to register an event handler in the DOM 0 event model. One of these is by assigning the event handler script to an event tag attribute, as in the following example: <input type = "button" id = "mybutton" onclick = "alert('you clicked my button!');" /> In many cases, the handler consists of more than a single statement. In these cases, often a function is used and the literal string value of the attribute is the call to the function. Consider the following example of a button element: <input type = "button" id = "mybutton" onclick = "mybuttonhandler();" /> An event handler function could also be registered by assigning its name to the associated event property on the button object, as in the following example: document.getelementbyid("mybutton").onclick = mybuttonhandler; This statement must follow both the handler function and the form element so that JavaScript has seen both before assigning the property. Notice that only the name of the handler function is assigned to the property it is neither a string nor a call to the function. 5 Handling Events from Body Elements The events most often created by body elements are load and unload. As our first example of event handling, we consider the simple case of producing an alert message when the body of the document has been loaded. In this case, we use the onload attribute of <body> to specify the event handler: 207