Events Comp 205 Fall 2017.

Slides:



Advertisements
Similar presentations
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Advertisements

The Web Warrior Guide to Web Design Technologies
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, Third Edition
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
Web Programming Material From Greenlaw/Hepp, In-line/On-line: Fundamentals of the Internet and the World Wide Web 1 Introduction The JavaScript Programming.
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.,
4.1 JavaScript Introduction
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
JavaScript 1. What is JavaScript? JavaScript allows web authors to create dynamic pages that react to user interaction. It is an Object-based because.
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.
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
Using Client-Side Scripts to Enhance Web Applications 1.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
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,
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
JavaScript Making Web pages come alive. Objectives Be able to read JavaScript scripts Be able to write JavaScript scripts.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
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.
Handling Image & Animation Using JavaScript HTML tag to display image: More options:
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Tarik Booker CS 120 California State University, Los Angeles.
XHTML Forms.
Module 1 Introduction to JavaScript
Chapter 5 Validating Form Data with JavaScript
Event-Driven Programming
JavaScript and HTML Simple Event Handling 11-May-18.
Applied Component I Unit II Introduction of java-script
Web Development & Design Foundations with HTML5
JavaScript is a programming language designed for Web pages.
Introduction to JavaScript Events
CS 330 Class 5 Programming plan for today: Questions on homework
Section 17.1 Section 17.2 Add an audio file using HTML
Web Development & Design Foundations with HTML5 7th Edition
JAVASCRIPTS AND HTML DOCUMENTS
14 A Brief Look at JavaScript and jQuery.
JavaScript and HTML Simple Event Handling 19-Sep-18.
DHTML Javascript Internet Technology.
Introduction to Programming the WWW I
Event Driven Programming & User Defined Functions
DHTML Javascript Internet Technology.
Introduction to JavaScript
JavaScript Basics What is JavaScript?
JavaScript is a scripting language designed for Web pages by Netscape.
Introduction to Programming and JavaScript
JavaScript and Ajax (JavaScript Events)
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

Events Comp 205 Fall 2017

JavaScript, Third Edition Objectives About events About HTML tags and events How to use event handlers About links How to use link events How to create an image map JavaScript, Third Edition

JavaScript, Third Edition Understanding Events Event: Specific circumstance monitored by JavaScript and that your script can respond to in some way, or A trigger that fires specific JavaScript code in response to a given situation e.g., an action that a user takes JavaScript, Third Edition

JavaScript, Third Edition Understanding Events Events Two basic types User-generated events e.g., mouse-click System-generated events e.g., load event  triggered by web browser when HTML document finishes loading JavaScript, Third Edition

Understanding Events (Cont.) JavaScript, Third Edition

JavaScript, Third Edition Elements and Events Events: Associated with XHTML elements Events available to an element vary: Click event available for the <a> element and from controls created with the <input> element In comparison, the <body> element does not have a click event, but does have a load event JavaScript, Third Edition

JavaScript, Third Edition Event Handlers Event handler: Code that executes in response to a specific event Code is included as an attribute of the element that initiates the event Syntax of an event handler within an element is: <element event_handler ="JavaScript code"> Event handler names are case sensitive: Must be written using all lowercase letters in order for a document to be well formed However, most JS interpreters will ignore capitalization. JavaScript, Third Edition

JavaScript, Third Edition Event Handlers Event handler naming convention Event name with a prefix of “on” E.g., onClick <input type=“button” onClick=“alert(‘You clicked the button!’)”> JavaScript, Third Edition

JavaScript, Third Edition Event Handlers (Cont.) JavaScript, Third Edition

JavaScript, Third Edition Using Events Event Handlers and functions Can use an event handler to call a function Allows one set of code to be used in many event handlers. JavaScript, Third Edition

Example: prompt.html <html> <head> <title>Prompt Box</title> <script type = "text/javascript"> <!-- document.writeln( "<h1>Welcome to JavaScript Programming!</h1>" ); function putWelcome() { var myName=prompt("What's your name?"); confirm("Is your name really " + myName + "?"); } // --> </script> </head> <body> <button name="tryMe" type="button" onClick="putWelcome(); "> Click Me!</button> </body> </html> When the click event is triggered, the function putWelcome() is called JavaScript, Third Edition

Example: prompt.html <html> <head> <title>Prompt Box</title> <script type = "text/javascript"> <!-- document.writeln( "<h1>Welcome to JavaScript Programming!</h1>" ); function putWelcome() { var myName=prompt("What's your name?"); confirm("Is your name really " + myName + "?"); } // --> </script> </head> <body> <button name="tryMe" type="button" onClick="putWelcome(); ">Click Me!</button> <button name="tryMe2" type="button" onClick="putWelcome(); ">Click Me too!</button> <button name="tryMe3" type="button" onClick="putWelcome(); ">Also click Me!</button> </body> </html> The same function can be used for multiple buttons JavaScript, Third Edition

JavaScript, Third Edition Using Events Event Handlers and functions May have multiple javaScript statements in a single event handler. May also return information from a function to an event handler. JavaScript, Third Edition

Example: prompt2.html Note that html commands can be used in the writeln function <html> <head> <title>Prompt Box</title> <script type = "text/javascript"> document.writeln("<h1>Welcome to JavaScript Programming!</h1>" ); function putWelcome() { var myName=prompt("What's your name?", "John"); confirm("Is your name really " + myName + "?"); // document.writeln("hello " + myName); return myName; } </script> </head> <body> <button name="tryMe" type="button" onClick="var theName = putWelcome(); document.writeln('<h1>Hello ' + theName + '</h1>');"> Click Me!</button> </body> </html> When the button is clicked, putWelcome is called putWelcome returns a string which is stored in the variable theName the variable theName is used in another statement in the event handler. JavaScript, Third Edition

JavaScript, Third Edition Using Events Links: review Used to open files or navigate to other documents on the web Text or image used to represent a link in an HTML document is called an anchor Use anchor tag <a> to process a URL JavaScript, Third Edition

JavaScript, Third Edition Using Events Links: review Two types of URL Absolute Refers to the specific location of the document http://www.site.com/index.html C:\webpages\homepage.html Relative Refers to location of document according to the location of currently loaded document /subdirectory/otherpage.html Increases portability of web site JavaScript, Third Edition

JavaScript, Third Edition Using Events Links Events Primary event is the click event For default operation, no event handler is required Overriding the default click event Add onClick event handler that executes custom code Event handler must return true or false true means to continue to follow the link false means to cancel the link Can use built-in confirm() method to generate Boolean value JavaScript, Third Edition

First warnUser is called warnUser puts up a prompt box. If the user clicks “ok”, the prompt returns true, otherwise returns false First warnUser is called Finally, the event handler returns the value that it received from warnUser to the browser and this determines whether the link is followed or not. To browser the value that the prompt returned (true or false) is then returned to the event handler. This statement is executed when the click event is triggered. JavaScript, Third Edition

JavaScript, Third Edition

JavaScript, Third Edition Using Events Examples: warnuser.html confirmLinks.html redPage.html JavaScript, Third Edition

JavaScript, Third Edition Using Events Links Events Other events: MouseOver event Occurs when the mouse moves over a link MouseOut event Occurs when the mouse moves off a link Can be used to change the text that appears in the browser’s status bar Use JavaScript status property for the window object JavaScript, Third Edition

JavaScript, Third Edition Using Events Links Events MouseOver event to change the status bar: onMouseOver = “window.status = ‘testing, testing, testing….’” JavaScript, Third Edition

JavaScript, Third Edition Using Events Links Events MouseOver event to change the status bar: Note that some browsers don’t handle onMouseOver changes to the status bar from inside image maps! JavaScript, Third Edition

JavaScript, Third Edition Using Events onMouseOver default behavior: display link in status bar if onMouseOver returns true tells the web browser not to perform default behavior if onMouseOver returns false, tells the web browser that it should perform default behavior. backwards from the onClick values! JavaScript, Third Edition

JavaScript, Third Edition Using Events default status bar message the defaultStatus property records the message that will always be displayed in the status bar unless another message is explicitly placed there. <body onLoad=“defaultStatus=‘The Dafoe Family’;”> JavaScript, Third Edition

JavaScript, Third Edition Using Events Examples: testStatusBar.html CorrectStatus.html BodyParts.html JavaScript, Third Edition

JavaScript, Third Edition Using Events Creating an Image Map: review An image divided into regions Each region associated with URL via the <a> tag Use <img>, <map>, and <area> tags to create an image map Areas are divided using pixel coordinates of image Picture elements JavaScript, Third Edition

JavaScript, Third Edition

JavaScript, Third Edition Using Events Creating an Image Map Two basic types Client-side Part of an HTML document Server-side Code resides on a web server JavaScript, Third Edition

JavaScript, Third Edition Using Events Creating an Image Map Process Place image in document using <img> tag Use usemap attribute to reference image map by name Define image map using <map> tag Use name attribute to give map a name Define image map regions using <area> tag Use shape attribute to specify shape of region rect, circle, poly JavaScript, Third Edition

JavaScript, Third Edition

JavaScript, Third Edition Image Maps next slide: use image map with mouseOver event Change status bar when mouseOver JavaScript, Third Edition

JavaScript, Third Edition

Image Maps Example: changing images with events. <img src=“north_america.gif”usemap=“#northAmerica_map” name=“northAmerica”> <map name=“northAmerica_map”> <area shape=“circle” coords=“44,46,20” nohref onMouseOver=“change_image(‘alaska.gif’);return false” onMouseOut=“reset_image(); return false”> <area shape=“poly” …… rest of html code here ……. </map> The image is given the name northAmerica The function change_image is called and the value alaska.gif is passed to the function. This event is triggered when the mouse is placed over the image. JavaScript, Third Edition

JavaScript, Third Edition Image Maps Example: changing images with events. <html> <head> <title>North America</title> <script language=“JavaScript”> <!-- hide from incompatible browsers function change_image(image_name){ document.northAmerica.src = image_name; } function reset_image(){ document.northAmerica.src = “north_america.gif” </script> </head> We change the src of the image named northAmerica to be the value that was passed into the function The src property or attribute of an image is the file name where the image lives. The function reset_image changes the image src back to “northamerica.gif”. We could have eliminated this function by calling change_image(“northamerica.gif”) from the onMouseOut event handler in the area tag. Every image is an object in the document. You can refer to the image by using the name given to it in the img tag JavaScript, Third Edition

JavaScript, Third Edition Using Events Examples: ShowCountry.html ShowAnimal.html ShowAnimal2.html (uses functions) FamilyImageMap.html JavaScript, Third Edition