JAVASCRIPTS AND HTML DOCUMENTS

Slides:



Advertisements
Similar presentations
5.1 JavaScript Execution Environment
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 9.
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Agenda Assignment one graded –Did not see any big issues »Some still have issues.
Computer Science 103 Chapter 4 Advanced JavaScript.
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.
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.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Chapter 5 © 2003 by Addison-Wesley, Inc. 1 Chapter 5 JavaScript and HTML Documents.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
Chapter 5 JavaScript and HTML Documents. © 2006 Pearson Addison-Wesley. All rights reserved JavaScript Execution Environment - The JavaScript.
JavaScript Part 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
Chapter 5 © 2012 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
CS346 Javascript-7A Events1 DOM Document Object Model and Events.
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.
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.
Chapter 5 © 2005 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
DHTML1-1 JavaScript and HTML Documents Xingquan (Hill) Zhu
Chapter 5 © 2014 by Pearson Education JavaScript Execution Environment - The JavaScript Window object represents the window in which the browser.
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.
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 5 © 2010 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
Thursday, August 6 th, 2015 Instructor: Craig Duckett Event Handling.
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.
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.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
H.Melikyan/4910/031 Programming the World Wide Web JavaScript Dr.Hayk Melikyan Departmen of Mathematics and CS JavaScript and HTML Documents.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
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.
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.
6.1 Introduction 6.2 Element Positioning
5.1 JavaScript Execution Environment
JavaScript and HTML Simple Event Handling 11-May-18.
Introduction to JavaScript Events
Chapter 14: DHTML: Event Model
Web Development & Design Foundations with HTML5 7th Edition
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript and HTML Documents
FORM OBJECT When creating an interactive web site for the Internet it is necessary to capture user input and process this input. Based on the result of.
CHAPTER 7 JavaScripts & HTML Documents
JavaScript and Events CS Programming Languages for Web Applications
5.1 JavaScript Execution Environment
JavaScript and Forms Kevin Harville.
JavaScript and Ajax (JavaScript Events)
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and Events CS Programming Languages for Web Applications
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

JAVASCRIPTS AND HTML DOCUMENTS CHAPTER 7 JAVASCRIPT II JAVASCRIPTS AND HTML DOCUMENTS

EXECUTION ENVIRONMENT 2 EXECUTION ENVIRONMENT The JavaScript Window object represents the window in which the browser displays documents The Window object provides the largest enclosing referencing environment for scripts Its properties are visible to all scripts in the document (they are the global) Other Window properties: document - a reference to the Document object that the window displays frames - an array of references to the frames of the document forms - an array of references to the forms of the document Each Form object has an elements array, which has references to the form’s elements Form elements are usually referenced by name, but this is a problem for radio buttons

DOCUMENT OBJECT MODEL Under development by w3c since the mid-90s DOM 0 is supported by all JavaScript browsers DOM 2 is the latest approved standard Nearly completely supported by NS6 IE6’s support is lacking some important things The DOM is an abstract model that defines the interface between HTML documents and application programs It is an OO model - document elements are objects A language that supports the DOM must have a binding to the DOM constructs In the JavaScript binding, HTML elements are represented as objects and element attributes are represented as properties e.g., <input type = "text" name = "address"> would be represented as an object with two properties, type and name, with the values "text" and "address“.  SHOW document & DOM tree

ELEMENT ACCESS IN JS There are several ways to do it DOM address 4 ELEMENT ACCESS IN JS There are several ways to do it Example (a document with just one form): <form action = ""> <input type = "button" name = "pushMe“ </form> DOM address document.forms[0].element[0] Problem: A change in the document could invalidate this address Element names – requires the element and all of its ancestors (except body) to have name attributes. Eg. : <form name = "myForm" action = ""> <input type = "button" name = "pushMe"> document.myForm.pushMe Problem: Strict standard does not allow form elements to have names

ELEMENT ACCESS IN JS 3. getElementById Method 5 Example: <form action = ""> <input type = "button" id = "pushMe"> </form> document.getElementById("pushMe")

EVENTS AND EVENT HANDLING 6 EVENTS AND EVENT HANDLING We look at the DOM 0 event model first In event-driven programming, code is executed as a result of a user or browser action An event is a notification that something specific has occurred, either with the browser or an action of the browser user An event handler is a script that is implicitly executed in response to the appearance of an event Because events are JavaScript objects, their names are case sensitive - all are in lowercase only The process of connecting an event handler to an event is called registration

EVENTS AND EVENT HANDLING 7 EVENTS AND EVENT HANDLING Event Tag Attribute abort onAbort blur onBlur change onChange click onClick error onError focus onFocus load onLoad mouseout onMouseOut mouseover onMouseOver reset onReset resize onResize select onSelect submit onSubmit unload onUnload

EVENTS AND EVENT HANDLING 8 EVENTS AND EVENT HANDLING The same attribute can appear in several different tags e.g., The onClick attribute can be in <a> and <input> A text element gets focus in three ways: When the user puts the mouse cursor over it and presses the left button When the user tabs to the element By executing the focus method Event handlers can be specified in two ways: By assigning the event handler script to an event tag attribute onClick = "alert('Mouse click!');“ onClick = "myHandler();“ Example: the load event - triggered when the loading of a document is completed

EVENTS AND EVENT HANDLING 9 EVENTS AND EVENT HANDLING <!-- load.html An example to illustrate the load events --> <html> <head><title> The onLoad event handler></title> <script type = "text/javascript"> <!-- // The onload event handler function load_greeting () { alert("You are visiting the home page of \n" + "Pete's Pickled Peppers \n" + "WELCOME!!!"); } // --> </script> </head> <body onload="load_greeting();"> </body></html>

EVENTS AND EVENT HANDLING 10 EVENTS AND EVENT HANDLING Radio buttons <input type = "radio" name = "button_group“ value = "blue" onClick = "handler()"> The checked property of a radio button object is true if the button is pressed Can’t use the element’s name to identify it, because all buttons in the group have the same name Must use the DOM address of the element, e.g., var radioElement = document.myForm.elements; Now we have the name of the array of elements for (var index = 0; index < radioElement.length; index++) { if (radioElement[index].checked) { element = radioElement[index].value; break; }

EVENTS AND EVENT HANDLING 11 EVENTS AND EVENT HANDLING Event handlers can be specified by assigning them to properties of the JavaScript objects associated with the HTML elements The property names are lowercase versions of the attribute names If the event handler is a function, just assign its name to the property, as in document.myForm.elements[0].onclick = myHandler; This sets the handler for the first element in the form This would need to follow both the handler function and the HTML form If this is done for a radio button group, each element of the array must be assigned - The disadvantage of specifying handlers by assigning them to event properties is that there is no way to use parameters

EVENTS AND EVENT HANDLING 12 EVENTS AND EVENT HANDLING The advantage of specifying handlers by assigning them to event properties are: It is good to keep HTML and JavaScript separate The handler could be changed during use Checking Form Input A good use of JavaScript, because it finds errors in form input before it is sent to the server for processing Things that must be done: Detect the error and produce an alert message Put the element in focus (the focus function) Select the element (the select function) The focus function puts the element in focus, which puts the cursor in the element document.getElementById("phone").focus(); The select function highlights the text in the element To keep the form active after the event handler is finished, have it return false

EVENTS AND EVENT HANDLING 13 EVENTS AND EVENT HANDLING Example – comparing passwords If a password will be used later, the user is asked to type it in twice The program must verify that the second typing of the password is the same as the first The form just has two password input boxes to get the passwords and Reset and Submit buttons The event handler is triggered by the Submit button Handler actions: If no password has been typed in the first box, focus on that box and return false If the two passwords are not the same, focus and select the first box and return false. if they are the same, return true --> SHOW pswd_chk.html

14 THE DOM2 EVENT MODEL Does not include DOM 0 features, but they are still supported Much more powerful than the DOM 0 model Microsoft does not support it, yet For more information, please refer to Sebesta book, page 198