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.

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.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Lesson 12- Unit L Programming Web Pages with JavaScript.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
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.
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.
JS: Document Object Model (DOM)
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 JavaScript and HTML Documents.
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.
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.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
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.
An Introduction to JavaScript Summarized from Chapter 6 of “Web Programming: Building Internet Applications”, 3 rd Edition.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
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.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
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.
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.
 defined as Extensible Markup Language (XML) is a set of rules for encoding documents  Defines structure and data.
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Web Programming Java Script & jQuery Web Programming.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
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.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
JavaScript محمد احمدی نیا 2 Of 48 What is JavaScript?  JavaScript was designed to add interactivity to HTML pages  A scripting.
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.
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
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.
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.
Copyright © Terry Felke-Morris Web Development & Design Foundations with HTML5 8 th Edition CHAPTER 14 KEY CONCEPTS 1 Copyright.
THE DOM.
DHTML.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
Introduction to the Document Object Model
Intro to JavaScript CS 1150 Spring 2017.
Web Development & Design Foundations with HTML5 7th Edition
JAVASCRIPTS AND HTML DOCUMENTS
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
DHTML Javascript Internet Technology.
DHTML Javascript Internet Technology.
CHAPTER 7 JavaScripts & HTML Documents
© 2015, Mike Murach & Associates, Inc.
Presentation transcript:

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.

What is the DOM?  The DOM is a W3C (World Wide Web Consortium) standard.  The DOM defines a standard for accessing documents like HTML and XML:  "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."  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 DOM defines the objects and properties of all document elements, and the methods (interface) to access them.

What is the HTML DOM?  The HTML DOM is:  A standard object model for HTML  A standard programming interface for HTML  Platform- and language-independent  A W3C standard  The HTML DOM defines the objects and properties of all HTML elements, and the methods(interface) to access them.  In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML elements.

DOM Nodes  According to the DOM, everything in an HTML document is a node.  The DOM says:  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

DOM Example  Look at the following HTML document:  DOM Tutorial DOM Lesson one Hello world!  The root node in the HTML above is. All other nodes in the document are contained within.  The node has two child nodes; and.  The node holds a node. The node holds a and node.

Text is Always Stored in Text Nodes  A common error in DOM processing is to expect an element node to contain text.  However, the text of an element node is stored in a text node.  In this example: DOM Tutorial, the element node, holds a text node with the value "DOM Tutorial".  "DOM Tutorial" is not the value of the element!  However, in the HTML DOM the value of the text node can be accessed by the innerHTML property.  You can read more about the innerHTML property in a later chapter.

The HTML DOM Node Tree  The HTML DOM views an HTML document as a tree-structure. The tree structure is called a node- tree.  All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created.  The node tree below shows the set of nodes, and the connections between them. The tree starts at the root node and branches out to the text nodes at the lowest level of the tree:

Node Parents, Children, and Siblings  The nodes in the node tree have a hierarchical relationship to each other.  The terms parent, child, and sibling are used to describe the relationships. Parent nodes have children. Children on the same level are called siblings (brothers or sisters).  In a node tree, the top node is called the root  Every node has exactly one parent node, except the root which has no parent  A node can have any number of children  A leaf is a node with no children  Siblings are nodes with the same parent

 The following image illustrates a part of the node tree and the relationship between the nodes:

HTML DOM Properties and Methods  Programming Interface  In the DOM, HTML documents consist of a set of node objects. The nodes can be accessed with JavaScript or other programming languages. In this tutorial we will use JavaScript.  The programming interface of the DOM is defined by standard properties and methods.  Properties are often referred to as something that is (i.e. the name of a node).  Methods are often referred to as something that is done (i.e. remove a node).

HTML DOM Properties  Some 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  Note: In the list above, x is a node object (HTML element).

HTML DOM Methods  Some 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  Note: In the list above, x is a node object (HTML element).

The innerHTML Property  The easiest way to get or modify the content of an element is by using the innerHTML property.  innerHTML is not a part of the W3C DOM specification. However, it is supported by all major browsers.  The innerHTML property is useful for returning or replacing the content of HTML elements (including and ), it can also be used to view the source of a page that has been dynamically modified.

Example  Hello World! txt=document.getElementById("intro").innerHTML; document.write(" The text from the intro paragraph: " + txt + " ");

childNodes and nodeValue  We can also use the childNodes and nodeValue properties to get the content of an element.  The following code gets the value of the element with id="intro":  Hello World! txt=document.getElementById("intro").childNodes[0].nod eValue; document.write(" The text from the intro paragraph: " + txt + " ");

HTML DOM Access Nodes  You can access a node in three ways:  By using the getElementById() method  By using the getElementsByTagName() method  By navigating the node tree, using the node relationships

The getElementById() Method  The getElementById() method returns the element with the specified ID:  Syntax  node.getElementById("id");  The following example gets the element with id="intro":  Example  document.getElementById("intro");

The getElementsByTagName() Method  getElementsByTagName() returns all elements with a specified tag name.  Syntax  node.getElementsByTagName("tagname");  The following example returns a nodeList of all elements in the document:  Example 1  document.getElementsByTagName("p");  The following example returns a nodeList of all elements that are descendants of the element with id="main":  Example 2  document.getElementById("main").getElementsByTagNa me("p");

DOM Node List  The getElementsByTagName() method returns a node-list. A node-list is an array of nodes.  The following code selects all nodes in a node-list:  Example  x=document.getElementsByTagName("p");  The nodes can be accessed by index number. To access the second you can write:  y=x[1];  Note: The index starts at 0.

DOM Node List Length  The length property defines the number of nodes in a node-list.  You can loop through a node-list by using the length property:  Example  x=document.getElementsByTagName("p"); for (i=0;i "); }  Example explained:  Get all element nodes  For each element, output the value of its text node

Navigating Node Relationships  The three properties; parentNode, firstChild, and lastChild, follow the document structure and allow short- distance travel in a document.  Look at the following HTML fragment:  Hello World! The DOM is very useful! This example demonstrates node relationships.

Example  Hello World! x=document.getElementById("intro"); document.write(x.firstChild.nodeValue);

DOM Root Nodes  There are two special document properties that allow access to the tags:  document.documentElement - returns the root node of the document  document.body - gives direct access to the tag

HTML DOM Node Information  Node Properties  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

The nodeName Property  The nodeName property 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  Note: nodeName always contains the uppercase tag name of an HTML element.

The nodeValue Property  The nodeValue property 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

Get the Value of an Element  The following example retrieves the text node value of the tag:  Example  Hello World! x=document.getElementById("intro"); document.write(x.firstChild.nodeValue);

The nodeType Property  The nodeType property returns the type of node. nodeType is read only.  The most important node types are: Element typeNodeType Element1 Attribute2 Text3 Comment8 Document9

HTML DOM - Change HTML Elements  Change an HTML Element  HTML DOM and JavaScript can change the inner content and attributes of HTML elements.  The following example changes the background color of the element:  Example  document.body.bgColor="lavender";

Change the Text of an HTML Element - innerHTML  The easiest way to get or modify the content of an element is by using the innerHTML property.  The following example changes the text of a element:  Example  Hello World! document.getElementById("p1").innerHTML="New text!";

Change an HTML Element Using Events  An event handler allows you to execute code when an event occurs.  Events are generated by the browser when the user clicks an element, when the page loads, when a form is submitted, etc.  You can read more about events in the next chapter.  The following example changes the background color of the element when a button is clicked:  Example 

Change the Text of an Element - with a Function  The following example uses a function to change the text of the element when a button is clicked:  Example  function ChangeText() { document.getElementById("p1").innerHTML="New text!"; } Hello world!

Using the Style Object  The Style object of each HTML element represents its individual style.  The following example uses a function to change the style of the element when a button is clicked:  Example  function ChangeBackground() { document.body.style.backgroundColor="lavender"; }

Change the font and color of an Element  The following example uses a function to change the style of the element when a button is clicked:  Example  function ChangeStyle() { document.getElementById("p1").style.color="blue"; document.getElementById("p1").style.fontFamily="Arial"; } Hello world!

HTML DOM - Events  Events  Every element on a web page has certain events which can trigger JavaScript functions. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML elements.  Examples of events:  A mouse click  A web page or an image loading  Mousing over a hot spot on the web page  Selecting an input box in an HTML form  Submitting an HTML form  A keystroke  Note: Events are normally used in combination with functions, and the function will not be executed before the event occurs!

onload and onUnload  The onload and onUnload events are triggered when the user enters or leaves a page.  The onload event is often used to check the visitor's browser type and version, and load the proper version of the web page based on that information.  Both the onload and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page. For example, you could have a popup asking for the user's name upon his first arrival to your page. The name is then stored in a cookie. Next time the visitor arrives at your page, you could have another popup saying something like: "Welcome John Doe!".

onFocus, onBlur and onChange  The onFocus, onBlur and onChange events are often used in combination with validation of form fields.  Below is an example of how to use an onChange event. The check () function will be called whenever the user changes the content of the field: 