Chapter 5 © 2005 by Addison Wesley Longman, Inc. 1 5.1 JavaScript Execution Environment - The JavaScript Window object represents the window in which the.

Slides:



Advertisements
Similar presentations
5.1 JavaScript Execution Environment
Advertisements

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Chapter 6 © 2012 by Addison Wesley Longman, Inc Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element.
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.
JavaScript Forms Form Validation Cookies CGI Programs.
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.
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Agenda Assignment 2 Posted –Program a web-based Version of Soduku using 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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 10.
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.
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.
1 Forms A form is the usual way that information is gotten from a browser to a server –HTML has tags to create a collection of objects that implement this.
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.
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.
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.
Using Client-Side Scripts to Enhance Web Applications 1.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Chapter 8 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 8 Sebesta: Programming the World Wide Web.
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.
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.
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.
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.
Chapter 6 © 2014 by Pearson Education Introduction Def: A dynamic HTML document is one whose tag attributes, tag contents, or element style properties.
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.
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Chapter 6 © 2003 by Addison-Wesley, Inc Introduction - Dynamic HTML is not a new markup language - A dynamic HTML document is one whose tag attributes,
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
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
Web Development & Design Foundations with HTML5 7th Edition
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript and HTML Documents
CHAPTER 7 JavaScripts & HTML Documents
5.1 JavaScript Execution Environment
JavaScript and Ajax (JavaScript Events)
6.1 Introduction 6.2 Positioning Elements
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

Chapter 5 © 2005 by Addison Wesley Longman, Inc JavaScript 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 - All global variables are properties of Window - Implicitly defined Window properties: - document - a reference to the Document object that the window displays - frames - an array of references to the frames of the document - Every Document object has: - 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 - Document also has anchors, links, & images

Chapter 5 © 2005 by Addison Wesley Longman, Inc The Document Object Model - DOM 0 is supported by all JavaScript-enabled browsers (no written specification) - DOM 1 was released in DOM 2 is the latest approved standard - Nearly completely supported by NS7 - IE6’s support is lacking some important things - The DOM is an abstract model that defines the interface between HTML documents and application programs—an API  SHOW document & DOM tree (p. 192)

Chapter 5 © 2005 by Addison Wesley Longman, Inc The Document Object Model - 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., would be represented as an object with two properties, type and name, with the values "text" and "address"

Chapter 5 © 2005 by Addison Wesley Longman, Inc Element Access in JavaScript - There are several ways to do it - Example (a document with just one form and one widget): 1. DOM address document.forms[0].element[0] Problem: document changes 2. Element names – requires the element and all of its ancestors (except body ) to have name attributes - Example: document.myForm.pushMe Problem: XHTML 1.1 spec doesn’t allow the name attribute on form elements

Chapter 5 © 2005 by Addison Wesley Longman, Inc Element Access in JavaScript (continued) 3. getElementById Method (defined in DOM 1) - Example: document.getElementById("pushMe") - Checkboxes and radio button have an implicit array, which has their name <input type = "checkbox" name = "toppings" value = "olives" />... <input type = "checkbox" name = "toppings" value = "tomatoes" />... var numChecked = 0; var dom = document.getElementById("topGroup"); for index = 0; index < dom.toppings.length; index++) if (dom.toppings[index].checked] numChecked++;

Chapter 5 © 2005 by Addison Wesley Longman, Inc Events and Event Handling - 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 - The process of connecting an event handler to an event is called registration - Don’t use document.write in an event handler, because the output may go on top of the display Event Tag Attribute blur onblur change onchange click onclick focus onfocus load onload mousedownonmousedown mousemoveonmousemove mouseout onmouseout mouseover onmouseover mouseuponmouseup select onselect submit onsubmit unload onunload

Chapter 5 © 2005 by Addison Wesley Longman, Inc Events and Event Handling (continued) - The same attribute can appear in several different tags e.g., The onclick attribute can be in and - A text element gets focus in three ways: 1. When the user puts the mouse cursor over it and presses the left button 2. When the user tabs to the element 3. By executing the focus method  SHOW Table 5.2

Chapter 5 © 2005 by Addison Wesley Longman, Inc Events and Event Handling (continued) - Event handlers can be registered in two ways: 1. By assigning the event handler script to an event tag attribute onclick = "alert('Mouse click!');" onclick = "myHandler();" 1

Chapter 5 © 2005 by Addison Wesley Longman, Inc Handling Events from Body Elements - Example: the load event - triggered when the loading of a document is completed  SHOW load.html & display 5.6 Handling Events from Button Elements - Plain Buttons – use the onclick property - Radio buttons - If the handler is registered in the markup, the particular button that was clicked can be sent to the handler as a parameter e.g., if planeChoice is the name of the handler and the value of a button is 172, use onclick = ″planeChoice(172)″ - This is another way of choosing the clicked button  SHOW radio_click.html & display

Chapter 5 © 2005 by Addison Wesley Longman, Inc Handling Events from Button Elements (continued) - If the handler is registered by assigning it to a property of the JavaScript objects associated with the HTML elements. As in: var dom = document.getElementById(″myForm″) dom.elements[0].onclick = planeChoice; - This registration must follow both the handler function and the XHTML form - Note that for the radio buttons, the checked property is used to determine whether a button is clicked - If the name of the buttons is planeButton var dom = document.getElementById(″myForm″); for (var index = 0; index < dom.planeButton.length; index++) { if (dom.planeButton[index].checked) { plane = dom.planeButton[index].value; break; } }  SHOW radio_click2.html 2

Chapter 5 © 2005 by Addison Wesley Longman, Inc Handling Events from Button Elements (continued) - The disadvantage of specifying handlers by assigning them to event properties is that there is no way to use parameters - The advantages of specifying handlers by assigning them to event properties are: 1. It is good to keep HTML and JavaScript separate 2. The handler could be changed during use 5.7 Handling Events from Textbox and Password Elements - The Focus Event - Can be used to detect illicit changes to a text box by blurring the element every time the element acquires focus  SHOW nochange.html

Chapter 5 © 2005 by Addison Wesley Longman, Inc Handling Events from Textbox and Password Elements (continued) - Checking Form Input - A good use of JavaScript, because it finds errors in form input before it is sent to the server for processing - So, it saves both: 1. Server time, and 2. Internet time - Things that must be done: 1. Detect the error and produce an alert message 2. Put the element in focus (the focus function) 3. 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

Chapter 5 © 2005 by Addison Wesley Longman, Inc Handling Events from Textbox and Password Elements (continued) - To keep the form active after the event handler is finished, the handler must return false - Problems: 1. With IE6, focus and select only work if the handler is registered by assigning it to the element event property 2. With NS7, select works, but focus does not - Example – comparing passwords - 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

Chapter 5 © 2005 by Addison Wesley Longman, Inc Handling Events from Textbox and Password Elements (continued) - Handler actions: 1. If no password has been typed in the first box, focus on that box and return false 2. 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 - Another Example – Checking the format of a name and phone number - The event handler will be triggered by the change event of the text boxes for the name and phone number - If an error is found in either, an alert message is produced and both focus and select are called on the text box element  SHOW validator.html

Chapter 5 © 2005 by Addison Wesley Longman, Inc The DOM 2 Event Model - Does not include DOM 0 features, but they are still supported by browsers - DOM 2 is modularized—one module is Events, which has two submodules, HTMLEvents and MouseEvents, whose interfaces are Event ( blur, change, etc.) and MouseEvent ( click, mouseup, etc.) - Event propagation - The node of the document tree where the event is created is called the target node - The capturing phase (the first phase) - Events begin at the root and move toward the target node - Registered and enabled event handlers at nodes along the way are run - The second phase is at the target node - If there are registered handlers there for the event, they are run - The third phase is the bubbling phase - Event goes back to the root; all encountered registered handlers are run

Chapter 5 © 2005 by Addison Wesley Longman, Inc The DOM 2 Event Model (continued) - Not all events bubble (e.g., load and unload ) - Any handler can stop further event propagation by calling the stopPropagation method of the Event object - DOM 2 model uses the Event object method, preventDefault, to stop default operations, such as submission of a form, if an error has been detected - Event handler registration is done with the addEventListener method - Three parameters: 1. Name of the event, as a string literal 2. The handler function 3. A Boolean value that specifies whether the event is enabled during the capturing phase node.addEventListener( "change", chkName, false);

Chapter 5 © 2005 by Addison Wesley Longman, Inc The DOM 2 Event Model (continued) - A temporary handler can be created by registering it and then unregistering it with removeEventListener - The currentTarget property of Event always references the object on which the handler is being executed - The MouseEvent interface (a subinterface of Event ) has two properties, clientX and clientY, that have the x and y coordinates of the mouse cursor, relative to the upper left corner of the browser window - An example: A revision of validator, using the DOM 2 event model  SHOW validator2.html - Note: DOM 0 and DOM 2 event handling can be mixed in a document

Chapter 5 © 2005 by Addison Wesley Longman, Inc The navigator object - Indicates which browser is being used - Two useful properties 1. The appName property has the browser’s name 2. The appVersion property has the version # - Microsoft has chosen to set the appVersion of IE6 to 4 (?) - Netscape has chosen to set the appVersion of NS7 to 5.0 (?)  SHOW navigate.html