Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –

Slides:



Advertisements
Similar presentations
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Advertisements

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Document Object Model (DOM) JavaScript manipulation of the DOM.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Tutorial 16 Working with Dynamic Content and Styles.
Tutorial 15 Working with the Event Model. XP Objectives Compare the IE and W3C event models Study how events propagate under both event models Write a.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 10.
XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Events.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Definition from Wikipedia.  The Prototype JavaScript Framework  implemented as a single file of JavaScript code  named prototype.js (
CST JavaScript Validating Form Data with JavaScript.
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.
JS: Document Object Model (DOM)
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
ITCS 6010 SALT. Speech Application Language Tags (SALT) Speech interface markup language Extension of HTML and other markup languages Adds speech and.
JQUERY | INTRODUCTION. jQuery  Open source JavaScript library  Simplifies the interactions between  HTML document, or the Document Object Model (DOM),
JavaScript – The DOM JavaScript is object based The browser is object based – We can access the browser's objects in the same way we did JavaScript's Two.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
JavaScript, Part 3 Instructor: Charles Moen CSCI/CINF 4230.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
XP Tutorial 16 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Dialog boxes in JavaScript Events in JavaScript – What are they – “Which events are there?” – “How do I register event handlers to an HTML element?” –
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
School of Computing and Information Systems CS 371 Web Application Programming JavaScript - DOM Modifying the Page from within.
INTRODUCTION JavaScript can make websites more interactive, interesting, and user-friendly.
4. Events are where it happens! Bear Bibeault Yehuda Katz.
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Host Objects: Browsers and the DOM
Understanding JavaScript and Coding Essentials Lesson 8.
HTML DOM Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
Introduction to JavaScript LIS390W1A Web Technologies and Techniques 24 Oct M. Cameron Jones.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
Jozef Goetz contribution, © by Pearson Education, Inc. All Rights Reserved.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Internet & World Wide Web How to Program, 5/e.  JavaScript events  allow scripts to respond to user interactions and modify the page accordingly  Events.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Introduction to JavaScript DOM Instructor: Sergey Goldman.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
THE DOM.
Introduction to JavaScript DOM and Events
Programming Web Pages with JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
CS 371 Web Application Programming
Introduction to JavaScript Events
Document Object Model (DOM): Objects and Collections
Working with the Event Model
Working with Dynamic Content and Styles
Chengyu Sun California State University, Los Angeles
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
Presentation transcript:

Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting – DOM2 Topic 4b: Event Handling and the DOM By the end of this lecture you should : - understand the benefits of the DOM -- know the node properties and methods -- be able to manipulate HTML content by accessing and changing the content of nodes -- Understand the basis of event handling -- be able to implement basic event handlers and listeners.

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Review: What is the DOM? What are the two key concepts behind the DOM?

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Recall the DOM treats all HTML pages as a collection of objects arranged in a hierarchy

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 The HTML document from the perspective of the DOM

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Node properties

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Node methods

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 So why do we bother with the properties and methods of nodes?

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 EXAMPLE – walking the DOM. Link to the HTML file which shows the code of the Example Example –click the link to run it in a browser and choose view source (in Firefox) or Open the example file in notepad to see the code

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Accessing nodes on the tree: The document.getElementById() Method Link to the HTML file which shows the code of the Example Link to the HTML file which shows the code of the Example – click the link to run it in a browser and choose view source (in Firefox) or Open the example file in notepad to see the code

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Accessing nodes on the tree: Link to the HTML file which shows the code of the Example Link to the HTML file which shows the code of the Example – click the link to run it in a browser and choose view source (in Firefox) or Open the example file in notepad to see the code The document.getElementsByTagName() Method

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 To modify the DOM (i.e. append, copy or remove elements) it is useful to know the DOM methods below:

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Easy way to modify the contents of an element: Example 1: Retrieving the contents of two paragraphs on an HTML page Example 1: Retrieving the contents of two paragraphs on an HTML page – the script grabs the content of two paragraphs and displays them in upper case at the bottom of the original paragraphs. Example 2: Changing the text after user interaction - click the links to run examples in a browser and choose view source (in Firefox) or Open the example file in notepad to see the code The innerHTML Property and the Element’s Content

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Creating new elements on the fly: ExampleExample– the element is created first and then appended to the document The createElement() method and createTextNode() method The insertBefore() method allows you to insert a child node before a specific node (called a reference node). If the reference node is null, this will insert the node at the end of a list of child nodes. The format is insertBefore(newElement, targetElement) Example

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Creating new elements on the fly: Example setAttribute() method creates a new attribute for an element. If an attribute with that name (nodeName) already exists in the element, it is replaced by the new one.

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Recall: What is an event? Consider: What are the main methods by which a user makes input?

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 Recall: What is an event? Consider: What are the main methods by which a user makes input? What does the user interact with? A list of standard events in javascript can be found here: asp See also ExampleExample of inline and scripting

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 DOM 2 – the flow of events

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 DOM 2 – the flow of events- how bubbling works

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 DOM 2 – how capturing works Example

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 DOM 2 – Stopping or cancelling event flow Example Event methods

Topic 4: The DOM and event handling Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling Inline Scripting DOM2 DOM 2 – Event listeners ExampleExample of event listener The addEventListener Method This method takes three arguments: 1. The event type to listen for (mouseover, click, etc.). 2. A function to be executed when the event is fired. 3. A Boolean (called useCapture) of true or false to specify the event propagation type: true to turn on event capturing and false to turn on event bubbling (the most cross-browser-compliant way is false). The addEventListener Method This method takes three arguments: 1. The event type to listen for (mouseover, click, etc.). 2. A function to be executed when the event is fired. 3. A Boolean (called useCapture) of true or false to specify the event propagation type: true to turn on event capturing and false to turn on event bubbling (the most cross-browser-compliant way is false). Using this within the Handler the this keyword, when used with W3C event handlers, is a reference to the HTML element from which the event handler was fired, giving you easy access to the element. ExampleExample of multiple events ExampleExample of removing listeners

Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting – DOM2 Topic 4b: Event Handling and the DOM By the end of this lecture you should : - understand the benefits of the DOM -- know the node properties and methods -- be able to manipulate HTML content by accessing and changing the content of nodes -- Understand the basis of event handling -- be able to implement basic event handlers and listeners.