1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.

Slides:



Advertisements
Similar presentations
Chapter 08: Adding Adding Interactivity Interactivity With With Behaviors Behaviors By Bill Bennett Associate Professor MSJC CIS MVC.
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
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.
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.
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.
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.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
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.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
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 JavaScript and HTML Documents. © 2006 Pearson Addison-Wesley. All rights reserved JavaScript Execution Environment - The JavaScript.
JavaScript Part 1.
CS346 Javascript-7A Events1 DOM Document Object Model and Events.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Dynamic HTML: Event Model Outline 16.1Introduction 16.2Event ONCLICK 16.3Event ONLOAD.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 14 - Dynamic HTML: Event Model Outline 14.1 Introduction 14.2 Event onclick 14.3 Event onload 14.4.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
Chapter 5 © 2005 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
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.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
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.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 14 - Dynamic HTML: Event Model Outline 14.1 Introduction 14.2 Event onclick 14.3 Event onload 14.4.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
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.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
20-753: Fundamentals of Web Programming 1 Lecture 13: Javascript II Fundamentals of Web Programming Lecture 13: Javascript II.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Copyright © Terry Felke-Morris Web Development & Design Foundations with HTML5 8 th Edition CHAPTER 14 KEY CONCEPTS 1 Copyright.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Methods and Object Information. Some Document Methods.
XHTML Forms.
JavaScript and HTML Simple Event Handling 11-May-18.
Introduction to JavaScript Events
Web Development & Design Foundations with HTML5 7th Edition
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and HTML Simple Event Handling 19-Sep-18.
Events Comp 205 Fall 2017.
JavaScript and Ajax (JavaScript Events)
Introduction to Web programming
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

1 CSC160 Chapter 7: Events and Event Handlers

2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler onLoad event handler onUnLoad event handler onFocus event handler onBlur event handler onChange event handler onSubmit event handler

3 Events and Event Handlers An event is something that happens when the viewer of a Web page performs some sort of action, including: Clicking a mouse button Clicking a button on the page Changing the contents of a form element Moving the mouse over a link on the page …… An event handler is a predefined JavaScript keyword that is used to handle an event on a Web page.

4 Why Event Handlers are Useful Event handlers enable us to gain access to the events that may occur on the page. Therefore, we can make things happen based on the actions of the viewer, which enables us to make more-interactive Web pages. For example, send an alert to the viewer when he or she moves the mouse over a link.

5 Event Handler Locations Where do we place the event handlers in an HTML document? Form elements Link tags The opening BODY tag Other areas

Forms An HTML form is a section of a document containing normal content, markup, special elements called controls (checkboxes, radio buttons, menus, etc.), and labels on those controls. Users "complete" a form by modifying its controls (entering text, selecting menu items, etc.). Users generally submit the form to an agent for processing (e.g., to a Web server, to a mail server, etc.). 6

Forms – Control Types buttons checkboxes radio buttons menus text input file select hidden controls object controls 7

8 onClick Event Handler The onClick event handler is used to make something happen when the viewer clicks a specific area of the Web page. In the following example, an alert window pops up whenever the viewer clicks the button element in the form. Tags are used to create a form in the web page The tag is used to create an element in the form The attribute type specifies the type of the element. In the example, we creates a button as an element of the form. Using the onClick event handler as an attribute requires you to use double quotes around all your JavaScript code, so when you need quote marks for the alert, use single quotes in order to avoid possible errors.

9 onClick Event Handler (cont’d) onclick0.htm The alert command ends with a semicolon. This enables you to add additional JavaScript code after the alert, which enables you to perform multiple actions on the click event rather than just a single JavaScript statement.

10 onClick Event Handler (cont’d) In the following example, multiple actions are performed on the click event rather than just a single JavaScript statement. onclick1.htm

11 onClick Event Handler (cont’d) If the code for an event handler is long, or if you will repeat it several times in the HTML document, you may put the code in a function. In the following example, we place three alerts within a function inside the HEAD section of the HTML document, and call the function from an event handler in the BODY section.

12 onClick Event Handler (cont’d) onclick2.htm <!-- function _3alerts() { window.alert('I told you not to click on me!'); window.alert(‘Be careful!’); window.alert(‘Bye!’); } //-->

13 onClick Event Handler (cont’d) In the following example, an alert pops up whenever the viewer clicks a link. onclick3.htm Eastern Kentucky University

14 onMouseOver Event Handler The mouseover event occurs when a viewer moves the mouse cursor over an area on the page such as a text link, linked image, linked portion of an image map. The mouseover event is handled with the onMouseOver event handler. onMouseOver.htm <A HREF=" onMouseOver="window.alert('I told you not to try to click me!');"> Don't Try Clicking Me!

15 onMouseOut Event Handler The mouseout event occurs when a viewer moves the mouse cursor away from an area on the page. The mouseover event is handled with the onMouseOut event handler. onMouseOut.htm <A HREF=" onMouseOut="window.alert(‘What, you don\’t like my link?');"> Click Me!

16 onLoad Event Handler The load event occurs when a Web page finishes loading. The load event is handled with the onLoad event handler which is placed in the opening BODY tag on a web page. onLoad.htm Text for the body of the page

17 onUnLoad Event Handler The unload event occurs when a viewer leaves the current Web page. The unload event is handled with the onUnLoad event handler which is placed in the opening BODY tag on a Web page. onUnLoad.htm Other HTML code goes here Eastern Kentucky University

18 onFocus Event Handler The focus event occurs when a viewer gives focus to a window or a form element on a web page. The viewer gives focus to something by clicking somewhere within the item, by using the keyboard to move to the item (often via the TAB key), or via a script. The focus event is handled with the onFocus event handler which can be placed in a form element or in the opening BODY tag on a web page (for focus on a window).

19 onFocus Event Handler (cont’d) onFocus.htm Enter Your Name: <INPUT type="text" onFocus="window.alert('Don\'t forget to capitalize!');">

20 onBlur Event Handler The blur event is the opposite of the focus event. It occurs when a viewer takes the focus away from a window or a form element on a Web page. To take the focus off something, the viewer usually gives focus to something else. The blur event is handled with the onBlur event handler which can be placed in a form element or in the opening BODY tag on a web page (for focus on a window).

21 onBlur Event Handler (cont’d) onblur.htm Give this box focus: Then give this box focus to blur the first one:

22 onChange Event Handler The change event occurs when a viewer changes something within a form element. For example, the viewer might change the text in a text box or make a selection from a select box. The change event is handled with the onChange event handler.

23 onChange Event Handler (cont’d) onChange.htm Would you like to sign up now? Yes No Undecided

24 onSubmit Event Handler The submit event only occurs when a viewer submits a form on a Web page. The submit event is handled with the onSubmit event handler, which can be called from an opening FORM tag.

25 onSubmit Event Handler (cont’d) onSubmit.htm What is your name?

26 Creating Scripts Using Event Handlers Problem 1: Change the text inside the status bar of the browser window. Problem 2: Make a form button into a button that acts like a link.

27 Status Bar Change The status bar is at the bottom of the browser window. We can change the text in the status bar of the browser, using a window property, window.status We need to return true in order to make sure the new text will override the original text that the browser displays.

28 Status Bar Change (cont’d) Status Bar <A href=" onMouseOver="window.status='The cursor is moved OVER the link'; return true;" onMouseOut="window.status='The cursor is moved OUT of the link'; return true;"> Eastern Kentucky University

29 Button Link Using JavaScript, we can turn a plain form button into a link, using another new property: window.location It enables us to change the URL address of the window in which you use it.

30 Button Link (cont’d) Button Link <INPUT Type="button" value="Go Searching!" onClick="window.location=' <INPUT Type="button" value="HTML Help!" onClick="window.location=' <INPUT Type="button" value="Some JavaScripts!" onClick="window.location='