Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54

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.
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.
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.
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.
COMP Week 4 Dr. Chunbo Chu Overview JavaScript Syntax Functions Objects Document Object Model Dynamic HTML.
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 and The Document Object Model MMIS 656 Web Design Technologies Acknowledgements: 1.Notes from David Shrader, NSU GSCIS 2.Some material adapted.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
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.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
JavaScript - A Web Script Language Fred Durao
Chapter 8 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 8 Sebesta: Programming the World Wide Web.
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.
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.
Chapter 5 © 2010 by Addison Wesley Longman, Inc JavaScript Execution Environment - The JavaScript Window object represents the window in which the.
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 
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
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.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
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.
Methods and Object Information. Some Document Methods.
6.1 Introduction 6.2 Element Positioning
5.1 JavaScript Execution Environment
JavaScript and HTML Simple Event Handling 11-May-18.
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:

Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54

Overview The Document Object Model (DOM) Element Access in JavaScript Events and Event Handling Handling events from Body Elements Handling events from Button Elements Handling events from Text Box and Password Elements Dynamic HTML Element positioning and moving Changing Colours and Fonts Dynamic Content Reacting to a Mouse Click

JavaScript Execution Environment The Window object provides the largest enclosing referencing environment for scripts 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 forms object has an elements array, which has references to the form’s elements Document also has property arrays for anchors, links, & images

The Document Object Model DOM 0 is supported by all JavaScript-enabled browsers (no written specification) DOM 1 was released in 1998 DOM 2 issued in 2000 Nearly completely supported by NS7 IE6’s support is lacking some important things DOM 3 is the latest W3C specification The DOM is an abstract model that defines the interface between HTML documents and application programs—an API

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"

DOM Structure Documents in the DOM have a tree like structure A simple document Breakfast 0 1 Lunch 1 0

Element Access in JavaScript There are several ways to do it 1. DOM address Example (a document with just one form and one widget): document.forms[0].elements[0] Problem: document changes

Element Access in JavaScript 2. Element names requires the element and all of its ancestors (except body) to have name attributes Example: document.myForm.pushMe

Element Access in JavaScript 3. getElementById Method (defined in DOM 1) Example: document.getElementById("pushMe") Form elements often have id s and name s both set to the same value

Element Access in JavaScript 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("toppingGroup"); for (index = 0; index < dom.toppings.length; index++) if (dom.toppings[index].checked] numChecked++;

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

Events and their Tag Attributes 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

Events, Attributes and Tags 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

Registration of Event Handler By assigning the event handler script to an event tag attribute

Handling Events from Body Elements Events most often created by body elements are load and unload Example: the load event - triggered when the loading of a document is completed

Handling Events from Button Elements Plain Buttons – use the onclick property Radio Buttons Example 1: The handler is registered in the markup, so the particular button that was clicked can be sent to the handler as a parameter Exampe 2: The handler is registered by assigning it to a property of the JavaScript objects associated with the HTML elements This registration must follow both the handler function and the HTML form

Handling Events from Textbox and Password Elements 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: 1.Detect the error and produce an alert message 2.Put the element in focus (the focus function) - puts the cursor in the element 3.Select the element (the select function) - highlights the text in the element To keep the form active after the event handler is finished, the handler must return false

Handling Events from Textbox and Password Elements Example 1 – comparing passwords The form just has two password input boxes and Reset and Submit buttons The event handler is triggered by the Submit button Example 2 – 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

Dynamic HTML An HTML document whose tag attributes, tag contents, or element style properties can be changed after the document has been and is still being displayed by a browser Such changes are made with an embedded script (JavaScript) that accesses the elements of the document as objects in the associated DOM structure

Element Positioning The position of any element is dictated by the three style properties: position, left, and top The three possible values of position are absolute, relative, and static <p style = "position: absolute; left: 50px; top: 100px;"> If position is set to either absolute or relative, the element can be moved after it is displayed Just change the top and left property values with a script

Changing Colours and Fonts Colour example: The actual parameter this.value works because at the time of the call, this is a reference to the text box (the element in which the call is made) So, this.value is the name of the new colour Changing fonts example We can change the font properties of a link by using the mouseover and mouseout events to trigger a script that makes the changes

Dynamic Content The content of an HTML element is addressed with the value property of its associated JavaScript object

Reacting to a Mouse Click A mouse click can be used to trigger an action, no matter where the mouse cursor is in the display Uses event handlers for onmousedown and onmouseup to change the visibility attribute of the message

Summary The Document Object Model (DOM) Element Access in JavaScript Events and Event Handling Handling events from Body Elements Handling events from Button Elements Handling events from Text Box and Password Elements Dynamic HTML Element positioning and moving Changing Colours and Fonts Dynamic Content Reacting to a Mouse Click