Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.

Slides:



Advertisements
Similar presentations
Essentials for Design JavaScript Level One Michael Brooks
Advertisements

Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
Session 11 Dynamic HTML: Event Model and Filters Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
The Web Warrior Guide to Web Design Technologies
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
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.
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.
JavaScript Client Side scripting. Client-side Scripts Client-side scripts, which run on the user’s workstation can be used to: Validate user inputs entered.
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
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.
HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Chapter 19: Adding JavaScript
Chapter 5 © 2003 by Addison-Wesley, Inc. 1 Chapter 5 JavaScript and HTML Documents.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
1 Dynamic HTML III: Event Model Introduction Event model –Scripts respond to user actions and change page accordingly Moving mouse Scrolling screen.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
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,
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
1 JavaScript 2 JavaScript. 2 Rollovers Most web page developers first use JavaScript for rollovers A rollover is a change in the appearance of an element.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
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.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Chapter 19 Creating and Processing HTML Forms. How HTML Forms Transmit Data name1=value1&name2=value2...&nameN =valueN GET or POST GET, an HTTP GET request,
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.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
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.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
Web Programming Java Script & jQuery Web Programming.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
5 th and 4 th ed: some from chapters 9, 12, 13 SY306 Web and Databases for Cyber Operations Slide Set #8: Dynamic HTML.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
JavaScript Events.
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.
Event Handlers Events are asynchronous. When an event occurs, JavaScript can execute code in response to the user’s action. This response to the user-initiated.
1 Lesson 6 Introducing JavaScript HTML and JavaScript BASICS, 4 th Edition.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Methods and Object Information. Some Document Methods.
JavaScript and HTML Simple Event Handling 11-May-18.
Introduction to JavaScript Events
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript Events.
JavaScript and Ajax (JavaScript Events)
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks

Copyright (c) 2004 Prentice-Hall. All rights reserved. 2 Objectives Detect mouse events Use events with the tag Apply blur and focus event handlers

Copyright (c) 2004 Prentice-Hall. All rights reserved. 3 Objectives (continued) Detect keyboard events Utilize form events Apply selection events Advanced use of event handlers

Copyright (c) 2004 Prentice-Hall. All rights reserved. 4 Why Would I Do This? Events are an important part of object-oriented programming Keywords that allow us to detect and react to events are called event handlers  Event handlers are occasionally referred to as listeners in advanced programming applications

Copyright (c) 2004 Prentice-Hall. All rights reserved. 5 Why Would I Do This? (continued) Event handlers are almost always used to "bind" HTML and JavaScript code together  Using JavaScript event handlers can be used to trigger actions when certain events are detected  Most practical uses of JavaScript require the detection of a specific event such as: when the user rolls over an image when a user submits a form

Copyright (c) 2004 Prentice-Hall. All rights reserved. 6 Visual Summary Event handlers are usually used inline with HTML tags The onclick event handler appears to be an HTML attribute of the tag Example Welcome to my site.

Copyright (c) 2004 Prentice-Hall. All rights reserved. 7 Visual Summary (continued) Bound tag Method Event handler End method quotes End event handler quotes

Copyright (c) 2004 Prentice-Hall. All rights reserved. 8 Visual Summary (continued) Event handlers always start with the word on followed by the event  This is typical of most object oriented languages Event handlers aren't case sensitive when used inline in HTML

Copyright (c) 2004 Prentice-Hall. All rights reserved. 9 Visual Summary (continued) Event handler statements are typically written in one of three ways: Type:Example Methodonclick="alert('hi');" User-Defined Functiononmouseover="myFunction();" Multiple Statements onkeypress="window.open(); x=10;"

Copyright (c) 2004 Prentice-Hall. All rights reserved. 10 Visual Summary (continued) In object oriented languages, events are tied to particular objects In JavaScript, event handlers are bound to HTML tags or the object that represents the HTML tag Each HTML tag has a list of event handlers which it can detect and to which it can respond

Copyright (c) 2004 Prentice-Hall. All rights reserved. 11 Detecting Mouse Events Events related to the use of a mouse were among the first event handlers to be added to JavaScript Newer versions of JavaScript contain event handlers to detect such things as the roll of a mouse wheel

Copyright (c) 2004 Prentice-Hall. All rights reserved. 12 Detecting Mouse Events (continued) OnClick and Ondblclick  The onclick event fires when an object is clicked  The ondblclick event fires when the user double clicks the mouse on an object  The onclick and ondblclick event handlers work on virtually any element that can be displayed on the screen

Copyright (c) 2004 Prentice-Hall. All rights reserved. 13 Detecting Mouse Events (continued) OnClick and Ondblclick  The onclick event fires when an object is clicked  The ondblclick event fires when the user double clicks the mouse on an object  The onclick and ondblclick event handlers work on virtually any element that can be displayed on the screen

Copyright (c) 2004 Prentice-Hall. All rights reserved. 14 Detecting Mouse Events (continued) ONMOUSEDOWN, ONMOUSEMOVE, ONMOUSEOUT, ONMOUSEOVER  The onmousedown event fires when the user clicks the mouse on an object  The onmousemove event fires when the mouse moves while over an element

Copyright (c) 2004 Prentice-Hall. All rights reserved. 15 Detecting Mouse Events (continued)  The onmouseover handler fires when the mouse initially moves over the edge of an element  The onmouseout event fires when the user moves the mouse off of an element <img src="normal version of the image;" onmouseover="switch the normal image with a rollover version;" onmouseout="remove the rollover image and return the original image;" onmousedown="go to the associated URL;">

Copyright (c) 2004 Prentice-Hall. All rights reserved. 16 Detecting Mouse Events (continued) Use OnClick  The onclick event fires when an object is clicked Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 17 Detecting Mouse Events (continued) onclick.html - shows the onclick event handler function eventTest() { alert("The function has been activated"); } This is text in a paragraph tag. this is a link

Copyright (c) 2004 Prentice-Hall. All rights reserved. 18 Detecting Mouse Events (continued) Detect Double Mouse Clicks  The onclick event fires when an object is double clicked Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 19 Detecting Mouse Events (continued) ondblclick.html - shows the ondblclick event handler function eventTest() { alert("The function has been activated"); } This is text in a paragraph tag. this is a link

Copyright (c) 2004 Prentice-Hall. All rights reserved. 20 Detecting Mouse Events (continued) Detecting Rollovers  The onmouseover event fires when the mouse initially moves over the edge of an element Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 21 Detecting Mouse Events (continued) onmouseover.html - shows the onmouseover event handler This page generates an alert box when someone rolls over the image shown below.

Copyright (c) 2004 Prentice-Hall. All rights reserved. 22 Using Events with the Tag The tag is a display tag which represents every item displayed in the active part of the browser window  Since the tag represents the page that is displayed, event handlers such as onclick or onmouseover can be used with the tag  Despite this ability, onclick or onmouseover rarely have practical applications when used with the

Copyright (c) 2004 Prentice-Hall. All rights reserved. 23 Using Events with the Tag (continued) Other event handlers can be used with the tag which are much more useful  The onload event handler can be used to trigger events a HTML page has finished loading into the browser

Copyright (c) 2004 Prentice-Hall. All rights reserved. 24 Using Events with the Tag (continued) onload  The onload handler fires when the last character of the HTML page has loaded  This event is typically used with the or tag but may also be attached to: tag

Copyright (c) 2004 Prentice-Hall. All rights reserved. 25 Using Events with the Tag (continued) onresize  This handler indicates the page is being resized  This event is typically used with the tag as in the following example  This event handler opens up all kinds of options for Web designers

Copyright (c) 2004 Prentice-Hall. All rights reserved. 26 Using Events with the Tag (continued) onunload  This event: fires when the browser is leaving the current document in a window or frame fires when the reset button is used, since the refresh button unloads the current document then loads a new version of the file can be used with the or tag  Example Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 27 Using Events with the Tag (continued) onunload.html This page uses the onunload event handler.

Copyright (c) 2004 Prentice-Hall. All rights reserved. 28 Using Events with the Tag (continued) Using Onresize  This handler indicates the page is being resized  This event is typically used with the tag as in the following example  Example of resizing the current document to a resolution of 640*480, approximately one second after the function is invoked Example of resizing the current document to a resolution of 640*480, approximately one second after the function is invoked

Copyright (c) 2004 Prentice-Hall. All rights reserved. 29 Using Events with the Tag (continued) At this point, nothing will happen since the function setsize() exists in the document but isn't been invoked anywhere in the code Next

Copyright (c) 2004 Prentice-Hall. All rights reserved. 30 Using Events with the Tag (continued) After you resize the window, the window should resize itself to a resolution of 640*480, approximately one second after you resize it

Copyright (c) 2004 Prentice-Hall. All rights reserved. 31 Using Events with the Tag (continued) Correct Event Detection Issues  If you will try to use self.resizeTo() without setTimeout() method, you may notice a "jerky" effect as the window constantly resizes as you try to resize it because the event is constantly being detected and acted upon  Example of resizing the current document to a resolution of 640*480, without the setTimeout() method Example of resizing the current document to a resolution of 640*480, without the setTimeout() method

Copyright (c) 2004 Prentice-Hall. All rights reserved. 32 Using Events with the Tag (continued) You may notice a "jerky" effect as the window constantly resizes as you try to resize it

Copyright (c) 2004 Prentice-Hall. All rights reserved. 33 Applying Blur and Focus Event Handlers The focus() and blur() methods can also be used with form elements The user can also create these events by choosing an object with the mouse

Copyright (c) 2004 Prentice-Hall. All rights reserved. 34 Applying Blur and Focus Event Handlers (continued) If a window or frame has focus, it means that any keystrokes we type will be directed to that window We can give focus to windows in a number of ways such as:  clicking in or on the window  using the focus() method

Copyright (c) 2004 Prentice-Hall. All rights reserved. 35 Applying Blur and Focus Event Handlers (continued) The onblur event fires when focus is given to another window  The onblur event can be used with the following tags:

Copyright (c) 2004 Prentice-Hall. All rights reserved. 36 Applying Blur and Focus Event Handlers (continued) The onfocus event fires when the user chooses the specified window  The onfocus event can be used with the following tags:,

Copyright (c) 2004 Prentice-Hall. All rights reserved. 37 Applying Blur and Focus Event Handlers (continued) If we open this page in the browser and choose another window, the event fires and the alert box appears

Copyright (c) 2004 Prentice-Hall. All rights reserved. 38 Applying Blur and Focus Event Handlers (continued) Detecting the Blur of a Window Object  The onblur and onfocus events can be used effectively to stop actions in a window  Example Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 39 Applying Blur and Focus Event Handlers (continued) onfocuselement.html Comments: <textarea name="comments" id="comments" tabindex="1" onblur="if (this.value=='') { alert('Please Enter a Comment');}" > Your name: document.form1.comments.focus();

Copyright (c) 2004 Prentice-Hall. All rights reserved. 40 Detecting Keyboard Events Keyboard events refer to a group of events related keys being pressed on the keyboard Keyboard events usually fire when the user either presses or releases a key Keyboard event handlers offer the opportunity to create usable experiences for disabled users who may have difficulty using a mouse

Copyright (c) 2004 Prentice-Hall. All rights reserved. 41 Detecting Keyboard Events (continued) ONKEYDOWN, ONKEYPRESS, ONKEYUP  The onkeydown event fires if a key is pressed while the element has focus  The event fires: as soon as the user presses the key and the key doesn't have to be released no matter which key is pressed

Copyright (c) 2004 Prentice-Hall. All rights reserved. 42 Detecting Keyboard Events (continued)  The onkeypress event fires if a key is pressed and then released while the element has focus The event:  doesn't fire until the key is released  fires no matter which key is pressed

Copyright (c) 2004 Prentice-Hall. All rights reserved. 43 Detecting Keyboard Events (continued)  The onkeyup event fires when a key is released when the element has focus the event doesn't fire until the key is released

Copyright (c) 2004 Prentice-Hall. All rights reserved. 44 Detecting Keyboard Events (continued) Detect a Keypress onkeypress.html - shows the onkeypress event handler Press a key while the link below is active and you will generate an alert box. Welcome to my site.

Copyright (c) 2004 Prentice-Hall. All rights reserved. 45 Use Form Related Events Forms offer very practical uses for various event Various techniques can be accomplished by detecting whether forms have been submitted or reset

Copyright (c) 2004 Prentice-Hall. All rights reserved. 46 Use Form Related Events (continued) onsubmit  The onsubmit handler indicates that a form is being submitted, usually by the press of a submit button  This event is usually bound to the tag Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 47 Use Form Related Events (continued) Name: <input type="submit" name="Submit" value="Submit"> <input type="reset" name="Reset“ value="Reset"> The code shown will create an alert box when the submit button is chosen

Copyright (c) 2004 Prentice-Hall. All rights reserved. 48 Use Form Related Events (continued) onreset  The onreset handler indicates that a form is being reset, usually by the press of a reset button  This event is usually bound to the tag Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 49 Use Form Related Events (continued) Name: The shown code will create an alert box when the reset button is chosen

Copyright (c) 2004 Prentice-Hall. All rights reserved. 50 Use Form Related Events (continued) Use the OnSubmit Event Handler  The onsubmit event handler can be used to detect when the user is ready to submit the contents of the form  Example Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 51 Use Form Related Events (continued) onsubmit.html - generates an alert when the form is submitted function formCheck() { alert("You entered "+document.form1.name.value);} Name: A function has been provided in the head of the document. This function will return any value entered into the form field

Copyright (c) 2004 Prentice-Hall. All rights reserved. 52 Use a Selection Event Event handlers exist which allow the developer to determine if text has been selected or changed These handlers are usually used within form elements for a variety of reasons

Copyright (c) 2004 Prentice-Hall. All rights reserved. 53 Use a Selection Event (continued) ONSELECT  This event fires when the user selects text in a form element by highlighting the text  It is available with the and tags Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 54 Use a Selection Event (continued)

Copyright (c) 2004 Prentice-Hall. All rights reserved. 55 Use a Selection Event (continued) onchange  The onchange event handler fires when the user modifies the value or content of an HTML element in a form: Select Input Text area then releases the mouse button  This handler is often used to check or confirm changes made the user

Copyright (c) 2004 Prentice-Hall. All rights reserved. 56 Use a Selection Event (continued) This will generate an alert box when the text is selected

Copyright (c) 2004 Prentice-Hall. All rights reserved. 57 Use a Selection Event (continued) Detect the Change of a Form Element  The ability to use the onchange event handler gives developers the ability to know if form elements have changed  This allows forms to be designed to help with complicated procedures

Copyright (c) 2004 Prentice-Hall. All rights reserved. 58 Use a Selection Event (continued) If you choose the Tab key to blur the input text box or click somewhere else to blur the text box the alert window appears

Copyright (c) 2004 Prentice-Hall. All rights reserved. 59 Advanced Use of Event Handlers You can also use event handlers outside of HTML tags by incorporating them directly into scripts You can also write code to manually fire many events in JavaScript without the event actually occurring Examples

Copyright (c) 2004 Prentice-Hall. All rights reserved. 60 Advanced Use of Event Handlers (continued) When the first HTML tag is pressed the text box the alert window appears Next

Copyright (c) 2004 Prentice-Hall. All rights reserved. 61 Advanced Use of Event Handlers (continued) Adding this line of code will cause the event to fire manually, even though the user hasn't clicked on the link

Copyright (c) 2004 Prentice-Hall. All rights reserved. 62 Advanced Use of Event Handlers (continued) Returning Information from an Event Handler  When you use the event handler as a method, you basically set the value of the condition to true, which allows JavaScript to execute the associated code  You can also set the event to false this has the effect of keeping the associated code from executing, even though the event has occurred Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 63 Advanced Use of Event Handlers (continued) The user must choose OK to return a true value and complete the form submission. If the user chooses Cancel, the return value is false, which effectively cancels the submit action Name:

Copyright (c) 2004 Prentice-Hall. All rights reserved. 64 Advanced Use of Event Handlers (continued) Create an Event Handler Using Dot Syntax Example

Copyright (c) 2004 Prentice-Hall. All rights reserved. 65 Advanced Use of Event Handlers (continued) When you move your mouse over the image the text box the alert window appears jsevent.html This file shows how to create an event handler in JavaScript. window.document.images[0].onmouseover =new Function("alert(You moved your mouse pointer over the image')");