DOM (Document Object Model) - Parsing and Reading HTML and XML -

Slides:



Advertisements
Similar presentations
The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
Advertisements

JavaScript Objects - DOM CST 200 JavaScript. Objectives Introduce JavaScript objects Introduce Document Object Model Introduce window object Introduce.
Page Elements © Copyright 2014, Fred McClurg All Rights Reserved.
Document Object Model (DOM) JavaScript manipulation of the DOM.
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.
Computer Information System Information System California State University Los Angeles Jongwook Woo CIS 461 Web Development I HTML DOM part II Jongwook.
Tutorial 16 Working with Dynamic Content and Styles.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
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.
1 XML Data Management 4. Domain Object Model Werner Nutt.
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.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
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.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
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.
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.
1 Dr Alexiei Dingli XML Technologies XML Advanced.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
XP Tutorial 16 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
 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.
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.
JavaScript DOM.
Markup basics. Markup languages add commentary to text files –so that the commentary can be ignored if not understood eg HyperText Markup Language –adds.
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.
 defined as Extensible Markup Language (XML) is a set of rules for encoding documents  Defines structure and data.
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM)
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
School of Computing and Information Systems CS 371 Web Application Programming JavaScript - DOM Modifying the Page from within.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
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.
IN THIS LESSON, WE WILL BECOME FAMILIAR WITH HTML AND BEGIN CREATING A WEB PAGE IN YOUR COMPUTER HTML – the foundation.
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.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
THE DOM.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
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
CGS 3066: Web Programming and Design Spring 2016
Intro to JavaScript CS 1150 Spring 2017.
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
The Document Object Model
DOM Document Object Model.
More Sample XML By Sadia Anjum.
CHAPTER 7 JavaScripts & HTML Documents
Introduction to Programming the WWW I
Document Object Model (DOM): Objects and Collections
2017, Fall Pusan National University Ki-Joune Li
Presentation transcript:

DOM (Document Object Model) - Parsing and Reading HTML and XML - 2012. 9 Youn-Hee Han LINK@KOREATECH http://link.koreatech.ac.kr

4/26/2017 What is DOM? The HTML DOM defines a standard for accessing and manipulating HTML documents. The DOM defines the objects and properties of all document elements, and the methods (interface) to access them. The DOM is a W3C (World Wide Web Consortium) standard. The DOM is separated into 3 different parts / levels Core DOM - standard model for any structured document XML DOM - standard model for XML documents HTML DOM - standard model for HTML documents The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document. LINK@KOREATECH

DOM Node In the DOM, everything in an HTML document is a node. 4/26/2017 DOM Node In the DOM, everything in an HTML document is a node. The entire document is a “document” node Every HTML element is an “element” node The text in the HTML elements are “text” nodes Every HTML attribute is an “attribute” node Comments are “comment” nodes <html>   <head>     <title>My Title</title>   </head>   <body> <a href="...">My Link</a>     <h1>My Header</h1>     </body> </html> The root node in the HTML above is <html>. The <html> node has two child nodes; <head> and <body>. The <head> node holds a <title> node. The <body> node holds a <a> and <h1> node. The <title> node holds a text node with the value “My Title". The <a> node holds a text node with “My Link” The <p> node holds a text node with “My Header” The <a> node holds an attribute node LINK@KOREATECH

Node Tree HTML DOM Node Tree 4/26/2017 Node Tree  HTML DOM Node Tree All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created. <html>   <head>     <title>My Title</title>   </head>   <body> <a href="...">My Link</a>     <h1>My Header</h1>     </body> </html> LINK@KOREATECH

Node Parents, Children, and Siblings 4/26/2017 Node Parents, Children, and Siblings  HTML DOM Node Tree Parent nodes have children. Children on the same level are called siblings Siblings are nodes with the same parent In a node tree, the top node is called the root Every node, except the root, has exactly one parent node A node can have any number of children A leaf is a node with no children LINK@KOREATECH

Programming Interface (1/2) 4/26/2017 Programming Interface (1/2) The programming interface of the DOM is defined by standard properties and methods. HTML DOM Properties x.innerHTML - the text value of x x.nodeName - the name of x x.nodeValue - the value of x x.parentNode - the parent node of x x.childNodes - the child nodes of x x.attributes - the attributes nodes of x LINK@KOREATECH

Programming Interface (2/2) 4/26/2017 Programming Interface (2/2) HTML DOM Methods x.getElementById(id) - get the element with a specified id x.getElementsByTagName(name) - get all elements with a specified tag name x.appendChild(node) - insert a child node to x x.removeChild(node) - remove a child node from x LINK@KOREATECH

DOM Method & Node Access Example 1 – innerHTML property <html> <body> <p id="intro">Hello World!</p> <script type="text/javascript"> txt=document.getElementById("intro").innerHTML; document.write("<p>The text from the intro paragraph: " + txt + "</p>"); </script> </body> </html> LINK@KOREATECH

DOM Method & Node Access Example 2 – getElementById method (1/2) <html> <body> <p id="intro">Hello World!</p> <script type="text/javascript"> txt=document.getElementById("intro").childNodes[0].nodeValue; document.write("<p>The text from the intro paragraph: " + txt + "</p>"); </script> </body> </html> LINK@KOREATECH

DOM Method & Node Access Example 3 – getElementById method (2/2) <html> <body> <p id="intro">Hello World!</p> <p>This example demonstrates the <b>getElementById</b> method!</p> <script type="text/javascript"> x=document.getElementById("intro"); document.write("<p>The text from the intro paragraph: " + x.innerHTML + "</p>"); </script> </body> </html> LINK@KOREATECH

DOM Method & Node Access Example 4 – getElementByTagName method (1/3) <html> <body> <p>Hello World!</p> <p>The DOM is very useful!</p> <p>This example demonstrates the <b>getElementsByTagName</b> method.</p> <script type="text/javascript"> x=document.getElementsByTagName("p"); document.write("Text of first paragraph: " + x[0].innerHTML + "<br />"); document.write("Text of second paragraph: " + x[1].innerHTML + "<br />"); document.write("Text of third paragraph: " + x[2].innerHTML + "<br />"); </script> </body> </html> LINK@KOREATECH

DOM Method & Node Access Example 5 – getElementByTagName method (2/3) <html> <body> <p>Hello World!</p> <p>The DOM is very useful!</p> <p>This example demonstrates the <b>getElementsByTagName</b> method.</p> <script type="text/javascript"> x=document.getElementsByTagName("p"); for (i=0;i<x.length;i++){ document.write(x[i].innerHTML); document.write("<br />"); } </script> </body> </html> LINK@KOREATECH

DOM Method & Node Access Example 6 – getElementByTagName method (3/3) <html> <body> <p>Hello World!</p> <div id="main"> <p>The DOM is very useful.</p> <p>This example demonstrates the <b>getElementsByTagName</b> method</p> </div> <script type="text/javascript"> x=document.getElementById("main").getElementsByTagName("p"); document.write("First paragraph inside the div: " + x[0].innerHTML); </script> </body> </html> LINK@KOREATECH

DOM Method & Node Access Each object has properties allowing us to access the lower level objects in the hierarchy. e.g., document.forms returns an array of forms in the document. access by position in array document.forms[0].elements[0].value Access by id/name in array document.forms["myForm"]["fname"].value Or just use id/name document.form1.text1.value LINK@KOREATECH

DOM Method & Node Access Example 7 – Node Access <html> <head> <script type="text/javascript"> function validateForm() { var x=document.forms["myForm"]["fname"].value; if (x==null || x=="") { alert("First name must be filled out"); return false; } </script> </head> <body> <form name="myForm" action="demo_form.jsp" onsubmit="return validateForm()" method="post"> First name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> </body> </html> 폼 전송을 하지 말것! LINK@KOREATECH

DOM Node Info. In the HTML DOM, each node is an object Objects have methods and properties that can be accessed and manipulated by JavaScript Three important node properties are nodeName nodeValue nodeType nodeName Property – It specifies the name of a node nodeName is read-only nodeName of an element node is the same as the tag name nodeName of an attribute node is the attribute name nodeName of a text node is always “#text” nodeName of the document node is always “#document” LINK@KOREATECH

DOM Node Info. nodeValue Property – It specifies the value of a node nodeValue for element nodes is undefined nodeValue for text nodes is the text itself nodeValue for attribute nodes is the attribute value nodeType Property – It specifies the type of node Element type NodeType Element 1 Attribute 2 Text 3 Comment 8 Document 9 LINK@KOREATECH

DOM Programming Example 8 – firstChild & nodeValue properties <html> <body> <p id="intro">Hello World!</p> <script type="text/javascript"> x=document.getElementById("intro"); document.write("<hr />"); document.write(x.nodeName + "<br />"); document.write(x.nodeValue + "<br />"); document.write(x.nodeType); document.write(x.firstChild.nodeName + "<br />"); document.write(x.firstChild.nodeValue + "<br />"); document.write(x.firstChild.nodeType); </script> </body> </html> LINK@KOREATECH

Attribute Accessing Example 9 – Accessing element attributes <html> <body> <img id="myimage" src="test.gif"> <script type="text/javascript"> var getAttrValue=document.getElementById("myimage").getAttribute("src") document.write(getAttrValue); </script> </body> </html> LINK@KOREATECH

Change HTML Element HTML elements can be changed using JavaScript, the HTML DOM and events HTML DOM and JavaScript can change the inner content and attributes of HTML elements <html> <head> <script type="text/javascript"> function ChangeStyle() { document.getElementById("p1").innerHTML="New text!"; document.getElementById("p1").style.color="blue"; document.getElementById("p1").style.fontFamily="Arial"; } </script> </head> <body> <p id="p1">Hello world!</p> <input type="button" onclick="ChangeStyle()" value="Change style" /> </body> </html> LINK@KOREATECH