 2008 Pearson Education, Inc. All rights reserved. 1 12 Document Object Model (DOM): Objects and Collections.

Slides:



Advertisements
Similar presentations
Document Object Model (DOM) JavaScript manipulation of the DOM.
Advertisements

Pemrograman Teknologi Internet W09: DOM & DHTML. 2 Objectives DOM: DOM: DOM Nodes & Trees DOM Nodes & Trees Traversing & Modifying DOM Trees Traversing.
Lesson 12- Unit L Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Chapter 9 Introduction to the Document Object Model (DOM) JavaScript, Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Cos 381 Day 10.
Chapter 2 Introduction to HTML5 Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
DHTML - Introduction Introduction to DHTML, the DOM, JS review.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
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)
Lecture 2 : Understanding the Document Object Model (DOM) UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 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.
CITS1231 Web Technologies JavaScript and Document Object Model.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
HTML, XHTML, and CSS Sixth Edition Chapter 1 Introduction to HTML, XHTML, and CSS.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 Introduction 34.2 Setup and Message.
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.
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.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
SDPL 2002Notes 3.2: Document Object Model1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications.
DHTML - Introduction Chapter Introduction to DHTML, the DOM, JS review.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 13 - Dynamic HTML: Object Model and Collections Outline 13.1 Introduction 13.2 Object Referencing.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
SDPLNotes 3.2: DOM1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications (parsers, browsers,
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.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
SDPL 2002Notes 3.2: Document Object Model1 The Misconceived Web n The original vision of the WWW was as a hyperlinked document-retrieval system. n It did.
HTML Concepts and Techniques Fifth Edition Chapter 1 Introduction to HTML.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 5. JavaScript and HTML Documents.
Host Objects: Browsers and the DOM
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
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.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Making web pages interactive with JavaScript.
XML DOM Week 11 Web site:
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.
REEM ALMOTIRI Information Technology Department Majmaah University.
THE DOM.
Introduction to JavaScript DOM and Events
DHTML.
Unit M Programming Web Pages with
Unit M Programming Web Pages with
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
HTML A brief introduction HTML.
Introduction to Programming the WWW I
Introduction to DHTML, the DOM, JS review
Presentation transcript:

 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections

 2008 Pearson Education, Inc. All rights reserved. 2 Our children may learn about heroes of the past. Our task is to make ourselves architects of the future. —Jomo Mzee Kenyatta Though leaves are many, the root is one. —William Butler Yeats The thing that impresses me most about America is the way parents obey their children. —Duke of Windsor Most of us become parents long before we have stopped being children. —Mignon McLaughlin To write it, it took three months; to conceive it three minutes; to collect the data in it—all my life. —F. Scott Fitzgerald Sibling rivalry is inevitable. The only sure way to avoid it is to have one child. —Nancy Samalin

 2008 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  How to use JavaScript and the W3C Document Object Model to create dynamic web pages.  The concept of DOM nodes and DOM trees.  How to traverse, edit and modify elements in an XHTML document.  How to change CSS styles dynamically.  To create JavaScript animations.

 2008 Pearson Education, Inc. All rights reserved Introduction 12.2 Modeling a Document: DOM Nodes and Trees 12.3 Traversing and Modifying a DOM Tree 12.4 DOM Collections 12.5 Dynamic Styles 12.6 Summary of the DOM Objects and Collections 12.7 Wrap-Up 12.8 Web Resources

 2008 Pearson Education, Inc. All rights reserved Introduction The Document Object Model gives you access to all the elements on a web page. Using JavaScript, you can create, modify and remove elements in the page dynamically.

 2008 Pearson Education, Inc. All rights reserved Modeling a Document: DOM Nodes and Trees getElementById method – Returns objects called DOM nodes – Every element in an XHTML page is modeled in the web browser by a DOM node The nodes in a document make up the page’s DOM tree, which describes the relationships among elements Nodes are related to each other through child-parent relationships A node may have multiple children, but only one parent Nodes with the same parent node are referred to as siblings Firefox’s DOM Inspector and the IE Web Developer Toolbar allow you to see a visual representation of a document’s DOM tree and information about each node The document node in a DOM tree is called the root node, because it has no parent

 2008 Pearson Education, Inc. All rights reserved. 7 Fig | Demonstration of a document’s DOM tree (Part 1 of 4). HTML element head element title element body element h1 element p element ul element li element

 2008 Pearson Education, Inc. All rights reserved. 8 Fig | Demonstration of a document’s DOM tree (Part 2 of 4).

 2008 Pearson Education, Inc. All rights reserved. 9 Fig | Demonstration of a document’s DOM tree (Part 3 of 4). HEAD and BODY nodes are siblings The BODY node is the parent of the H1 node

 2008 Pearson Education, Inc. All rights reserved. 10 Fig | Demonstration of a document’s DOM tree (Part 4 of 4).

 2008 Pearson Education, Inc. All rights reserved Traversing and Modifying a DOM Tree The className property of a DOM node allows you to change an XHTML element’s class attribute The id property of a DOM node controls an element’s id attribute

 2008 Pearson Education, Inc. All rights reserved. 12 Software Engineering Observation 12.1 With the DOM, XHTML elements can be treated as objects, and many attributes of XHTML elements can be treated as properties of those objects. Then, objects can be scripted (through their id attributes) with JavaScript to achieve dynamic effects.

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 13 Document Object Model (DOM) How to provide uniform access to structured documents in diverse applications (parsers, browsers, editors, databases)? Overview of W3C DOM Specification –second one in the “XML-family” of recommendations Level 1, W3C Rec, Oct Level 2, W3C Rec, Nov Level 3, W3C Working Draft (January 2002) What does DOM specify, and how to use it?

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 14 DOM: What is it? An object-based, language-neutral API for XML and HTML documents –allows programs and scripts to build documents, navigate their structure, add, modify or delete elements and content –Provides a foundation for developing querying, filtering, transformation, rendering etc. applications on top of DOM implementations

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 15 DOM structure model Based on O-O concepts: –methods (to access or change object’s state) –interfaces (declaration of a set of methods) –objects (encapsulation of data and methods) Roughly similar to the XSLT/XPath data model (to be discussed later)  a parse tree –Tree-like structure implied by the abstract relationships defined by the programming interfaces; Does not necessarily reflect data structures used by an implementation (but probably does)

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 16 Document Tree Structure document document.body document. documentElement

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 17 child, sibling, parent

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 18 child, sibling, parent

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 19 child, sibling, parent

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 20 child, sibling, parent

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 21 DOM interfaces: Node invoice invoicepage name addressee addressdata address form="00" type="estimatedbill" Leila Laskuprintti streetaddresspostoffice KUOPIOPyynpolku 1NodegetNodeTypegetNodeValuegetOwnerDocumentgetParentNode hasChildNodesgetChildNodes getFirstChildgetLastChildgetPreviousSiblinggetNextSibling hasAttributesgetAttributes appendChild(newChild)insertBefore(newChild,refChild)replaceChild(newChild,oldChild)removeChild(oldChild)Document Element NamedNodeMap Text

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 22 Object Creation in DOM Each DOM object X lives in the context of a Document: X.getOwnerDocument() Objects implementing interface X are created by factory methods D.create X (…), where D is a Document object. E.g: – createElement("A"), createAttribute("href"), createTextNode("Hello!") Creation and persistent saving of Document s left to be specified by implementations

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 23 invoice invoicepage name addressee addressdata address form="00"type="estimatedbill" Leila Laskuprintti streetaddresspostoffice KUOPIO Pyynpolku 1 DocumentgetDocumentElementcreateAttribute(name)createElement(tagName)createTextNode(data)getDocType()getElementById(IdVal) Node Document Element NamedNodeMap Text DOM interfaces: Document

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 24 DOM interfaces: Element invoice invoicepage name addressee addressdata address form="00"type="estimatedbill" Leila Laskuprintti streetaddresspostoffice KUOPIO Pyynpolku 1 ElementgetTagNamegetAttributeNode(name)setAttributeNode(attr)removeAttribute(name)getElementsByTagName(name)hasAttribute(name) Node Document Element NamedNodeMap Text

 2008 Pearson Education, Inc. All rights reserved. SDPL 2002 Notes 3.2: Document Object Model 25 DOM: Implementations Java-based parsers e.g. IBM XML4J, Apache Xerces, Apache Crimson MS IE5 browser: COM programming interfaces for C/C++ and MS Visual Basic, ActiveX object programming interfaces for script languages XML::DOM (Perl implementation of DOM Level 1) Others? Non-parser-implementations? (Participation of vendors of different kinds of systems in DOM WG has been active.)

 2008 Pearson Education, Inc. All rights reserved Traversing and Modifying a DOM Tree (Cont.) document object createElement method – Creates a new DOM node, taking the tag name as an argument. Does not insert the element on the page. document object createTextNode method – Creates a DOM node that can contain only text. Given a string argument, createTextNode inserts the string into the text node. Method appendChild – Called on a parent node to insert a child node (passed as an argument) after any existing children parentNode property of any DOM node contains the node’s parent insertBefore method – Called on a parent with a new child and an existing child as arguments. The new child is inserted as a child of the parent directly before the existing child. replaceChild method – Called on a parent, taking a new child and an existing child as arguments. The method inserts the new child into its list of children in place of the existing child. removeChild method – Called on a parent with a child to be removed as an argument.

 2008 Pearson Education, Inc. All rights reserved. 27 Fig | Basic DOM functionality (Part 1 of 14). Creates a class to highlight text

 2008 Pearson Education, Inc. All rights reserved. 28 Fig | Basic DOM functionality (Part 2 of 14). Calls function switchTo if the object can be found Calls function createNewNode to make a new p node with the text in the ins text field Inserts newNode as a child of the parent node, directly before currentNode Highlights newNode with function switchTo Inserts newNode as a child of the current node

 2008 Pearson Education, Inc. All rights reserved. 29 Fig | Basic DOM functionality (Part 3 of 14). Gets the parent of currentNode, then inserts newNode into its list of children in place of currentNode Ensures that top-level elements are not removed Removes oldNode from the document Highlights oldNode ’s parent Gets the parent node Makes sure the parent is not the body element

 2008 Pearson Education, Inc. All rights reserved. 30 Fig | Basic DOM functionality (Part 4 of 14). Creates (but does not insert) a new p node Creates a unique id for the new node Creates new text node with the contents of variable text, then inserts this node as a child of newNode Changes class attribute to unhighlight old node Highlights currentNode Assigns currentNode ’s id to the input field’s value property

 2008 Pearson Education, Inc. All rights reserved. 31 Fig | Basic DOM functionality (Part 5 of 14).

 2008 Pearson Education, Inc. All rights reserved. 32 Fig | Basic DOM functionality (Part 6 of 14).

 2008 Pearson Education, Inc. All rights reserved. 33 Fig | Basic DOM functionality (Part 7 of 14).

 2008 Pearson Education, Inc. All rights reserved. 34 Fig | Basic DOM functionality (Part 8 of 14).

 2008 Pearson Education, Inc. All rights reserved. 35 Fig | Basic DOM functionality (Part 9 of 14).

 2008 Pearson Education, Inc. All rights reserved. 36 Fig | Basic DOM functionality (Part 10 of 14).

 2008 Pearson Education, Inc. All rights reserved. 37 Fig | Basic DOM functionality (Part 11 of 14).

 2008 Pearson Education, Inc. All rights reserved. 38 Fig | Basic DOM functionality (Part 12 of 14).

 2008 Pearson Education, Inc. All rights reserved. 39 Fig | Basic DOM functionality (Part 13 of 14).

 2008 Pearson Education, Inc. All rights reserved. 40 Fig | Basic DOM functionality (Part 14 of 14).

 2008 Pearson Education, Inc. All rights reserved DOM Collections DOM has collections—groups of related objects on a page DOM collections are accessed as properties of DOM objects such as the document object or a DOM node The document object has properties containing the images collection, links collection, forms collection and anchors collection – Contain all the elements of the corresponding type on the page To find the number of elements in the collection, use the collection’s length property

 2008 Pearson Education, Inc. All rights reserved DOM Collections (Cont.) Access items in a collection via square brackets item method of a DOM collection – Access specific elements in a collection, taking an index as an argument namedItem method – takes a name as a parameter and finds the element in the collection, if any, whose id attribute or name attribute matches it href property of a DOM link node – Refers to the link’s href attribute Collections allow easy access to all elements of a single type in a page – Useful for gathering elements into one place and for applying changes across an entire page

 2008 Pearson Education, Inc. All rights reserved. 43 Fig | Using the links collection (Part 1 of 3). Stores the document’s links collection in variable linkslist Number of elements in the collection

 2008 Pearson Education, Inc. All rights reserved. 44 Fig | Using the links collection (Part 2 of 3). Stores the current link in currentLink Uses the link method to create an anchor element with proper text and href attribute Puts all links in one location by inserting them into an empty div element The document’s links

 2008 Pearson Education, Inc. All rights reserved. 45 Fig | Using the links collection (Part 3 of 3).

 2008 Pearson Education, Inc. All rights reserved Dynamic Styles An element’s style can be changed dynamically – E.g., in response to user events – Can create many effects, including mouse hover effects, interactive menus, and animations body property of the document object – Refers to the body element in the XHTML page style property – can access a CSS property in the format node.style.styleproperty. CSS property with a hyphen ( - ), such as background-color, is referred to as backgroundColor in JavaScript – Removing the hyphen and capitalizing the first letter of the following word is the convention for most CSS properties

 2008 Pearson Education, Inc. All rights reserved. 47 Fig | Dynamic styles (Part 1 of 2). Prompts the user for a color Sets the background-color CSS property to the user’s color

 2008 Pearson Education, Inc. All rights reserved. 48 Fig | Dynamic styles (Part 2 of 2).

 2008 Pearson Education, Inc. All rights reserved Dynamic Styles (Cont.) setInterval method of the window object – Repeatedly executes a statement on a certain interval – Takes two parameters A statement to execute repeatedly An integer specifying how often to execute it, in milliseconds – Returns a unique identifier to keep track of that particular interval. window object’s clearInterval method – Stops the repetitive calls of object’s setInterval method – Pass to clearInterval the interval identifier that setInterval returned

 2008 Pearson Education, Inc. All rights reserved. 50 Fig | Dynamic styles used for animation (Part 1 of 7).

 2008 Pearson Education, Inc. All rights reserved. 51 Fig | Dynamic styles used for animation (Part 2 of 7). Stops the animation when the image has reached its full size Sets properties for the new img node Swaps newNode for the old cover node Keeps aspect ratio consistent

 2008 Pearson Education, Inc. All rights reserved. 52 Fig | Dynamic styles used for animation (Part 3 of 7). Executes function run every 10 milliseconds

 2008 Pearson Education, Inc. All rights reserved. 53 Fig | Dynamic styles used for animation (Part 4 of 7).

 2008 Pearson Education, Inc. All rights reserved. 54 Fig | Dynamic styles used for animation (Part 5 of 7).

 2008 Pearson Education, Inc. All rights reserved. 55 Fig | Dynamic styles used for animation (Part 6 of 7).

 2008 Pearson Education, Inc. All rights reserved. 56 Fig | Dynamic styles used for animation (Part 7 of 7).

 2008 Pearson Education, Inc. All rights reserved Summary of the DOM Objects and Collections The objects and collections in the W3C DOM give you flexibility in manipulating the elements of a web page. The W3C DOM allows you to access every element in an XHTML document. Each element in a document is represented by a separate object. For a reference on the W3C Document Object Model, see the DOM Level 3 recommendation from the W3C at The DOM Level 2 HTML Specification, available at describes additional DOM functionality specific to HTML, such as objects for various types of XHTML elements. Not all web browsers implement all features included in the DOM specification.

 2008 Pearson Education, Inc. All rights reserved. 58 Fig | W3C Document Object Model.

 2008 Pearson Education, Inc. All rights reserved. 59 Fig | Objects and collections in the W3C Document Object Model (Part 1 of 2).

 2008 Pearson Education, Inc. All rights reserved. 60 Fig | Objects and collections in the W3C Document Object Model (Part 2 of 2).