Event Handling (the right way). A Simple Web Page Events - Summary <input type="button" id="btnMyButton" value="Click Me!"> The web page looks like this:

Slides:



Advertisements
Similar presentations
Session Variables Storing Session Variables on the Server.
Advertisements

The Web Warrior Guide to Web Design Technologies
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
JavaScript- Processing HTML Forms. Event Handlers Begins with and ends with.
JavaScript- Introduction. What it is and what it does? What it is? It is NOT Java It is NOT Server-side programming Users can see code It is a client-side.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
JavaScript Lesson 1 TBE 540. Prerequisites  Before beginning this lesson, the learner must be able to… Create a basic web page using a text editor and/or.
 JavaScript is a programming language that web browsers understand. You can use it to make your web pages interactive by: Responding to user actions and.
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
JavaScript Events and Event Handlers 1 An event is an action that occurs within a Web browser or Web document. An event handler is a statement that tells.
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.,
JavaScript Form Validation
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Intro to JavaScript. Use the tag to tell the browser you are writing JavaScript.
Using JavaScript in FORMs Fort Collins, CO Copyright © XTR Systems, LLC Learning to Use JavaScript in HTML FORMs Instructor: Joseph DiVerdi, Ph.D., MBA.
Announcements Assignment 9 is due Project 2 is due 7/27/04 We will be skipping chapter 22 Test 3 (chapters 18-21) will be on 7/29/04 Tip of the Day : Functions.
Ajax - 1h. AJAX IS: A browser technology A technology that uses only JavaScript in the browser page A very efficient way of updating information A way.
GEMVC. The Setup Folders Views Value Objects (VOs) Custom Events Service CFCs Controller Model Application Main MXML.
JavaScript - A Web Script Language Fred Durao
PHP Form Introduction Getting User Information Text Input.
Button and Textbox. Input  Input objects are used to obtain input from the user viewing the webpage. They allow the user to interact with the Web. 
1 JavaScript
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
Dialog boxes in JavaScript Events in JavaScript – What are they – “Which events are there?” – “How do I register event handlers to an HTML element?” –
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Event-Driven Programming Chapter Page Section: Section or division of an HTML page Generic block element Example: What's the difference between.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
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.
1 JavaScript Part 3. Functions Allow the user to decide when a particular script should be run by the browser in stead of running as long as the page.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
JavaScript Challenges Answers for challenges
CIS 375—Web App Dev II JavaScript I. 2 Introduction to DTD JavaScript is a scripting language developed by ________. A scripting language is a lightweight.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
ASP.NET User Controls. User Controls In addition to using Web server controls in your ASP.NET Web pages, you can create your own custom, reusable controls.
Introduction to Programming and JavaScript. Programming.
Javascript JavaScript is what is called a client-side scripting language:  a programming language that runs inside an Internet browser (a browser is also.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
USING JAVASCRIPT TO SHOW AN ALERT Web Design Sec 6-2 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development.
Understanding JavaScript and Coding Essentials Lesson 8.
Chapter 4 Java Script - Part1. 2 Outlines Introduction to Java Script Variables, Operators and Functions Conditional statements JavaScript Objects Forms.
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.
JQUERY AND AJAX
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
JavaScript JavaScript is a programming language that web browsers understand. You can use it to make your web pages interactive by: Responding to user.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Client-side processing 26 Client-side processing 26.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
1 Project 8: Tic Tac Toe. 2 Assignment Write an HTML and JavaScript page that pemits users to play Tic Tac Toe. Both players use a single browser. Alternating.
Build in Objects In JavaScript, almost "everything" is an object.
Using JavaScript to Show an Alert
HTML & teh internets.
JavaScript is a programming language designed for Web pages.
© 2015, Mike Murach & Associates, Inc.
BASIC HTML CODING BY cHRIS JACKSON.
Introduction to JavaScript
A second look at JavaScript
Javascript: variables and parameters
Event Driven Programming & User Defined Functions
Functions, Regular expressions and Events
Events: Changed and Input
Introduction to JavaScript
Tutorial 10: Programming with javascript
JavaScript Basics What is JavaScript?
JavaScript functions.
Retrieving information from forms
Presentation transcript:

Event Handling (the right way)

A Simple Web Page Events - Summary <input type="button" id="btnMyButton" value="Click Me!"> The web page looks like this: And the HTML looks like this: Pretty simple - just one button…

Our objective is to make something happen when the user clicks on the button. The “click” is the event that we’re waiting for. We need to run some code when the user clicks the button. The code that we run will be in a JavaScript function. We call this function the event handler. It will handle the click event for our button. To keep things simple, let’s place our JavaScript code in the same file with the HTML: … Events - Summary function HandleMyButtonsClick() { alert("Thanks for clicking me!"); } … This function is our event handler. It displays a simple alert. This function is our event handler. It displays a simple alert. But right now, it will NOT RUN. It needs to be “hooked up” to our button. We have to tell the function to run when the button gets clicked. Let’s do that now… But right now, it will NOT RUN. It needs to be “hooked up” to our button. We have to tell the function to run when the button gets clicked. Let’s do that now…

… var btnMyButton = document.getElementByID(“btnMyButton”); btnMyButton.onclick = HandleMyButtonsClick; function HandleMyButtonsClick() { alert("Thanks for clicking me!"); } … Hook Up the Event Handler Function to the Button Grab hold of the button object and assign it to a variable. This line of code says, “In the EVENTuality that btnMyButton gets clicked, call the HandleMyButtonsClick function.” Be sure that these 4 names all match up! Be sure that these 2 names match up!

… var btnMyButton = document.getElementByID(“btnMyButton”); btnMyButton.onclick = HandleMyButtonsClick; function HandleMyButtonsClick() { alert("Thanks for clicking me!"); } … Now let’s make the code more elegant by eliminating the variable, and using an anonymous function… document.getElementByID(“btnMyButton”).onclick = function() { alert("Thanks for clicking me!"); }; There’s no need to declare a variable to hold the object if you’re only going to refer to the object just once in your code. There’s no need to declare a variable to hold the object if you’re only going to refer to the object just once in your code. Anonymous functions are almost always the way to go when creating event handlers. Anonymous functions are almost always the way to go when creating event handlers.

Thanks for watching.