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.

Slides:



Advertisements
Similar presentations
Document Object Model. Lecture 18 The Document Object Model (DOM) is not a programming language It is an object-oriented model of web documents Each.
Advertisements

Tutorial 16 Working with Dynamic Content and Styles.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
DHTML - Introduction Introduction to DHTML, the DOM, JS review.
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.
4.1 JavaScript Introduction
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.
Dynamic HTML. A combination of technologies to create dynamic web pages – xhtml – CSS – Javascript Browsers make the page that is being displayed, its.
Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
D2L Notes Be sure to submit your link in the dropbox provided on D2L You can just upload an empty text file if a file upload is required Do not use D2L.
Creating an Animated Web Page
Working with Objects Creating a Dynamic Web Page.
CITS1231 Web Technologies JavaScript and Document Object Model.
XP Tutorial 4 New Perspectives on JavaScript, Comprehensive1 Working with Objects Creating an Animated Web Page.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COP 4814 – Web Services Dr. Roy Levow Part 4 - XML.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
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.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
DHTML: Working with Objects Creating a Dynamic Web Page.
Navigating the DOM with ExtJS By Aaron Conran. Document Object Model The Document Object Model or DOM is a standard to represent HTML, XHTML and other.
XP Tutorial 16 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
Markup basics. Markup languages add commentary to text files –so that the commentary can be ignored if not understood eg HyperText Markup Language –adds.
DYNAMIC HTML What is Dynamic HTML: HTML code that allow you to change/ specify the style of your web pages. Example: specify style sheet, object model.
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 –
Document Object Model. Back to the DOM… Javascript and the DOM  Originally, the Document Object Model (DOM) and Javascript were tightly bound  Each.
XML DOM.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
School of Computing and Information Systems CS 371 Web Application Programming JavaScript - DOM Modifying the Page from within.
Document Object Model CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also from.
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Wednesday, February 19, 2014 Session 4: JavaScript.
Computer Information System Information System California State University Los Angeles Jongwook Woo CIS 461 Web Development I HTML DOM part I Jongwook.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
XML DOM Week 11 Web site:
DOM Dr. Reda Salama. Back to the HTML DOM Once we get a response back from the server, we probably want to update our HTML page The HTML page itself is.
Introduction to JavaScript DOM Instructor: Sergey Goldman.
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
DHTML.
>> JavaScript: Document Object Model
CHAPTER 5 DOCUMENT OBJECT MODEL
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
CGS 3066: Web Programming and Design Spring 2017
Applied Online Programming
CS 371 Web Application Programming
Introduction to the Document Object Model
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
DOM Document Object Model.
Introduction to Programming the WWW I
Document Object Model (DOM): Objects and Collections
Introduction to DHTML, the DOM, JS review
Presentation transcript:

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. Scott Fitzgerald

3 DOM & DHTML Dynamic web pages with JavaScript and DOM –DHTML (Dynamic HTML) DOM nodes and DOM tree Traversing, editing and modifying DOM nodes Editing text nodes Accessing, editing and modifying elements' attributes

4 DOM Concept DOM makes all components of a web page accessible –HTML elements –their attributes –text They can be created, modified and removed with JavaScript

5 DOM Objects DOM components are accessible as objects or collections of objects DOM components form a tree of nodes relationship parent node – children nodes document is the root node Attributes of elements are accessible as text Browsers can show DOM visually as an expandable tree –Firebug for Firefox –in IE -> Tools -> Developer Tools

6 DOM Standards W3C defines the standards DOM Level 3 recommendation – DOM Level 2 HTML Specification – –additional DOM functionality specific to HTML, in particular objects for XHTML elements But, the developers of web browsers –don't implement all standards –implement some standards differently –implement some additional features

7 Accessing Nodes by id Access to elements by their id – document.getElementById( ) returns the element with id – id attribute can be defined in each start tag div element with id attribute can be used as an root node for a dynamic DOM subtree span element with id attribute can be used as a dynamic inline element The preferred way to access elements

8 Other Access Methods Access by elements' tag –there are typically several elements with the same tag – document.getElementsByTagName( ) returns the collection of all elements whose tag is the collection has a length attribute an item in the collection can be reached by its index –e.g. var html = document.getElementsByTagName("html")[0]; Access by elements' name attribute –several elements can have the same name – document.getElementsByName( ) returns the collection of elements with name

9 Traversing DOM tree Traversal through node properties – childNodes property the value is a collection of nodes –has a length attribute –an item can be reached by its index e.g. var body = html.childNodes[1]; – firstChild, lastChild properties – nextSibling, previousSibling properties – parentNode property

10 Other Node Properties nodeType property ELEMENT_NODE : HTML element TEXT_NODE : text within a parent element ATTRIBUTE_NODE : an attribute of a parent element –attributes can be accessed another way CDATA_SECTION_NODE –CDATA sections are good for unformatted text – nodeName property – nodeValue property – attributes property – innerHTML property not standard, but implemented in major browsers very useful – style property object whose properties are all style attributes, e.g., those defied in CSS

11 Accessing JS Object's Properties There are two different syntax forms to access object's properties in JS ( –. dot notation, e.g., document.nodeType – [ ] brackets notation, e.g., document["nodeType"] this is used in for-in loops this works for properties of DOM objects, too

12 Attributes of Elements Access through attributes property – attributes is an array –has a length attribute –an item can be reached by its index –an item has the properties name and value –e.g. var src = document.images[0].attributes[0].value; Access through function getAttribute( ) –returns the value of attribute –e.g. var src = document.images[0].getAttribute("src");

13 Text Nodes Text node –can only be as a leaf in DOM tree –it’s nodeValue property holds the text – innerHTML can be used to access the text Watch out: –There are many more text nodes than you would expect!

14 Modifying DOM Structure document.createElement( ) –creates a new DOM element node, with tag. –the node still needs to be inserted into the DOM tree document.createTextNode( ) –creates a new DOM text with –the node still needs to be inserted into the DOM tree.appendChild( ) –inserts node behind all existing children of node.insertBefore(, ) –inserts node before child within node.replaceChild(, ) –replaces child by node within node.removeChild( ) –removes node from within node

15 Modifying Node Attributes.setAttribute(, ) –sets the value of attribute to –e.g. document.images[0].setAttribute("src","keiki.jpg"); That's the standard –but it doesn't work in IE, there you have to use setAttribute( ) –e.g. document.images[0].setAttribute("src=\"keiki.jpg\"");

16 W3C Document Object Model

17 Special DOM Objects window –the browser window –new popup window s can be opened document –the current web page inside the window body – element of the document history –sites that the user visited –makes it possible to go back and forth using scripts location –URL of the document –setting it goes to another page