JavaScript and Ajax (JavaScript Events)

Slides:



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

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.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
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.
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.
JavaScript - a Brief Introduction Anupriya. What is JavaScript Object based (not object oriented) programming language –very limited object creation –a.
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
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.
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.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
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.
Thursday, August 6 th, 2015 Instructor: Craig Duckett Event Handling.
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.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
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.
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.
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.
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.
Third lecture Event 27/2/2016 JavaScript Tutorial.
XHTML Forms.
5.1 JavaScript Execution Environment
JavaScript and HTML Simple Event Handling 11-May-18.
In this session, you will learn to:
JavaScript - a Brief Introduction
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.
JavaScript Events.
JavaScript and HTML Documents
Event Driven Programming & User Defined Functions
Events Comp 205 Fall 2017.
CHAPTER 7 JavaScripts & HTML Documents
5.1 JavaScript Execution Environment
JavaScript Examples 27-Apr-19.
JavaScript Examples 30-Apr-19.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
JAVA SCRIPT OBJECTS & DOM
Barb Ericson Georgia Institute of Technology May 2006
JavaScript Examples 12-Jul-19.
Introduction to Web programming
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

JavaScript and Ajax (JavaScript Events) Week 8 Web site: http://fog.ccsf.edu/~hyip

Event and Event Handler Events: are visitor and browser activities. (the phone rings) Event handlers: are the mechanisms that allow us to capture and actually respond to those events with a scripting language. (pick up the phone and say, “Hello”)

Writing Event Handlers Event handlers: are written inline with HTML, just like an HTML attribute. Therefore, Event handlers also called intrinsic event attributes. (the only different is that Event Handler executes JavaScript script or function). HTML Tag: <p align="right"> Event handler/intrinsic event attribute: <body onload="alert('Hello')">

Events and Event Handling JavaScript programs use an event-driven programming model. The web browser generates an event whenever something interesting happens to the document or to some element of it. For example, the web browser generates an event when it finishes loading a document, when the user moves the mouse over a hyperlink, or when the user clicks on a button in a form If a JavaScript application cares about a particular type of event for a particular document element, it can register an event handler – a JavaScript function or snippet of code – for that type of event on the element of interest. Then, when the particular event occurs, the browser invokes the handler code. All application with graphical user interfaces are designed this way: they sit around waiting for the user to do something interesting (i.e. they wait for events to occur), and then they respond.

Events and Event Handling (continue…) Three distinct and incompatible event-handling models are in used: The original event model The standard event model The Internet Explorer event model

Basic Event Handling (continue…) In the original event model, event-handling code is specified using the attributes of HTML elements. Thus, if your application needs to know when the user moves the mouse over a specific hyperlink, you use the onmouseover attribute of the <a> tag that defines the hyperlink. If the application needs to know when the user clicks the Submit button, you use the onclick attribute of the <input> tag that defines the button or the onsubmit attribute of the <form> element that contains that button.

Basic Event Handling (continue…) There are quite a few different event-handler attributes that you can use in the original event model. They are : onblur onchange onclick onfocus onkeydown onkeypress onkeyup onload onmousedown onmousemove onmouseout onmouseover onmouseup onsubmit onunload

Event Handler Return Values In many cases, an event handler uses its return value to indicate the disposition of the event. For example, if you use the onsubmit event handler of a Form object to perform form validation and discover that the user has not filled in all the fields, you can return false from the handler to prevent the form from actually being submitted: <form action = "search.cgi" onsubmit="if (this.elements[0].value.length == 0) return false; "> <input type = "text"> </form>

Event Handlers and the this Keyword When your event handler is invoked, it is invoked as a method of the element on which the event occurred, so the this keyword refers to that target element: <input type="button" value="press me" onclick = "callfunc(this);"> // the this keyword refers to the Button object.

The Pseudo-protocol & the void operator The JavaScript pseudo-protocol (javascript:) in the href attribute of an <a> or <area> tag: the idea is that instead of requesting a document, the JavaScript pseudo-protocol will instead execute one or more JavaScript statements, which may or may not return a URL to the href. The void operator: tells the interpreter to evaluate an expression and return no value. void (expression) or void expression NOTE: void is an operator, not a function, where expression is an expression to be evaluated. Parentheses are optional. You want to make certain that a statement called via the pseudo-protocol does not return a value and provoke the link to load a new document indicated by the returned value. The void operator will ensure that no value is returned to the hypertext link at all.

Intrinsic Event Attributes Load onLoad (associated with windows, images) Unload onUnload (associated only with windows) Click onClick (associated with any elements) MouseOver onMouseOver (associated with any elements) MouseOut onMouseOut (associated with any elements) Focus onFocus (associated with windows, frames, links, and form elements) Blur onBlur (associated with windows, frames, links, and form elements) Submit onSubmit (associated with forms) Reset onReset (associated with forms) Select onSelect (associated with text boxes, password, text areas) Change onChange (associated with text boxes, text areas, password, file uploads, select lists)

Event - onload <html> <head> <script language= "JavaScript" type="text/javascript"> function mymessage() { alert("This message was triggered from the onload event"); } </script> </head> <body onload="mymessage()"> </body> </html>

Event - onunload <html> <head> <script type="text/javascript"> function mymessage() { alert("This message was triggered from the onunload event"); } </script> </head> <body onunload="mymessage()"> <p>An alert box will display a message when you close this document!</p> </body> </html>

Event - onchange <html> <head> <script type="text/javascript"> function preferedBrowser() { prefer=document.forms[0].browsers.value; alert("You prefer browsing internet with " + prefer); } </script> </head> <body> <form> Choose which browser you prefer: <select id="browsers" onchange="preferedBrowser()"> <option value="Internet Explorer">Internet Explorer <option value="Netscape">Netscape </select> </form> </body> </html>

Event - onsubmit <html> <head> <script type="text/javascript"> function confirmInput() { fname=document.forms[0].fname.value; alert("Hello " + fname + "! You will now be redirected to www.ccsf.edu"); } </script> </head> <body> <form onsubmit="confirmInput()" action="http://www.ccsf.edu/"> Enter your name: <input id="fname" type="text" size="20"> <input type="submit"> </form> </body> </html>

Event - onblur <html> <head> <script type="text/javascript"> function message() { alert("This alert box was triggered by the onblur event handler"); } </script> </head> <body> <p>The onblur event occurs when an element loses focus. Try to click or write in the input field below, then click elsewhere in the document so the input field loses focus.</p> <form> Enter your name: <input type="text" onblur="message()" size="20"> </form> </body> </html>

Event - onfocus <html> <head> <script type="text/javascript"> function message() { alert("This alert box was triggered by the onfocus event handler"); } </script> </head> <body> <form> Enter your name: <input type="text" onfocus="message()" size="20"> </form> </body> </html>

Event – onmouseover & onmouseout <html> <body> <h1 onmouseover="style.color='red'" onmouseout="style.color='black'"> Mouse over this text</h1> </body> </html>

Event - onclick <html> <head> <script type="text/javascript"> function disp_func() { Alert(“This alert box was triggered by the onclick event handler”); } </script> </head> <body> <input type=“button” value=click to display message” onclick=“disp_func();”> </body> </html>

Event - onmousemove <html> <head> <script type="text/javascript"> var i=1; function moveright() { document.getElementById('header').style.position="relative"; document.getElementById('header').style.left=i; i++; } </script> </head> <body onmousemove="moveright()"> <h1 id="header“>Move the mouse over this page</h1> </body> </html>