JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.

Slides:



Advertisements
Similar presentations
The Document Object Model
Advertisements

HTML: HyperText Markup Language Hello World Welcome to the world!
WeB application development
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.
Tutorial 16 Working with Dynamic Content and Styles.
XML DOM and SAX Parsers By Omar RABI. Introduction to parsers  The word parser comes from compilers  In a compiler, a parser is the module that reads.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
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)
1 The Document Object Model SAMS Teach Yourself JavaScript in 24 Hours (Fourth Edition) Michael Moncur SAMS Publishing 2007.
 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.
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.
CITS1231 Web Technologies JavaScript and Document Object Model.
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.
Client side web programming Introduction Jaana Holvikivi, DSc. School of ICT.
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.
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 IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
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.
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.
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.
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.
XML DOM.
CO1552 – Web Application Development Further JavaScript: Part 1: The Document Object Model Part 2: Functions and Events.
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.
LING 408/508: Programming for Linguists Lecture 11 October 5 th.
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.
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.
Department of Computer Science, Florida State University CGS 3066: Web Programming and Design Spring
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
Department of Computer Science, Florida State University CGS 3066: Web Programming and Design Spring
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Making web pages interactive with JavaScript.
CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.
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.
Advanced Topics in Concurrency and Reactive Programming: HTML, CSS and DOM Trees Majeed Kassis.
THE DOM.
>> JavaScript: Document Object Model
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
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
Working with Dynamic Content and Styles
Document Object Model (DOM): Objects and Collections
LING 408/508: Computational Techniques for Linguists
CSc 337 Lecture 9: The DoM tree.
Presentation transcript:

JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and allows programmers generic access to: –adding, deleting, and manipulating - of all styles, attributes, and elements in a document. –It can be accessed via any language available in the browser, including Java, JavaScript/ECMAScript/JScript, and VBScript (MSIE only). –The DOM is supported most completely starting in IE 5 and Gecko (NS6 and upwards, such as Firefox.)

JS: Document Object Model (DOM) Every tag, attribute, style, and piece of text is available to be accessed and manipulated via the DOM -- the possibilities are endless: Every tag, attribute, style, and piece of text is available to be accessed and manipulated via the DOM -- the possibilities are endless: –adding and removing tags, –attributes and styles, –animating existing elements, –and hiding/ showing elements on a page.

JS: Document Object Model (DOM) The DOM is constantly being revised by the W3C, with browsers at the same time constantly trying to support the latest recommended version of the DOM. The DOM is constantly being revised by the W3C, with browsers at the same time constantly trying to support the latest recommended version of the DOM. W3C Before we get started, you need to know a few terms that we will use: Before we get started, you need to know a few terms that we will use: –Node: A reference to an element, its attributes, or text from the document. –Element: A representation of a. –Attribute: A property of an element. HREF is an attribute of, for example.

JS: Document Object Model (DOM) The DOM represent an HTML documents as a tree of objects. The DOM represent an HTML documents as a tree of objects. The tree representation of an HTML document contains nodes representing HTML tags or elements, such as and, and nodes representing strings of text. The tree representation of an HTML document contains nodes representing HTML tags or elements, such as and, and nodes representing strings of text. An HTML document may also contain nodes representing HTML comments. An HTML document may also contain nodes representing HTML comments.

JS: Document Object Model (DOM) Consider the following simple HTML document: Consider the following simple HTML document:

JS: Document Object Model (DOM) The tree representation of the previous HTML document: The tree representation of the previous HTML document:

JS: Document Object Model (DOM) The node relationships of the previous DOM tree: The node relationships of the previous DOM tree: –HTML is an ancestor for head, h1, i, … etc –head and body are sibling –h1 and p are children of body –head is parent of title –“this is a” is the first children of p –head is the firstChild of html

JS: Document Object Model (DOM) The DOM says that: The DOM says that: –The entire document is a document node –Every HTML tag is an element node –The texts contained in the HTML elements are text nodes –Every HTML attribute is an attribute node –Comments are comment nodes

JS: Document Object Model (DOM) Activity 11:1 Basic Node Properties (DOM) Basic Node Properties (DOM) –nodeName is the name of the node (not the ID). The name is the tag name for HTML tag nodes, The name is the tag name for HTML tag nodes, #document for the document node, and #document for the document node, and #text for text nodes. #text for text nodes. –nodeType is a number describing the node's type: 1 for HTML tags, 1 for HTML tags, 3 for text nodes, and 3 for text nodes, and 9 for the document. 9 for the document. –nodeValue is the text contained within a text node. –id is the value of the ID attribute for the node.

JS: Document Object Model (DOM) Activity 11:2 Relationship Properties Relationship Properties –firstChild is the first child node for the current node. –lastChild is the last child object for the current node. –childNodes is an array of all the child nodes under a node. –previousSibling is the sibling before the current node. –nextSibling is the sibling after the current node. –parentNode is the object that contains the current node.

JS: Document Object Model (DOM) Activity 11:3 – node editing Node Methods Node Methods –appendChild(node) adds a new child node to the node after all its existing children. –insertBefore(node, oldnode) inserts a new node before the specified existing child node. –replaceChild(node, oldnode) replaces the specified old child node with a new node. –removeChild(node) removes an existing child node. –hasChildNodes() returns a Boolean value of true if the node has one or more children, or false if it has none. –cloneNode() returns a copy of the current node.

JS: Document Object Model (DOM) innerHTML innerHTML sets or gets all of the markup and content within a given element. innerHTML sets or gets all of the markup and content within a given element. var markup = element.innerHTML; element.innerHTML = markup; var markup = element.innerHTML; element.innerHTML = markup; Creating Some text Creating Some text –Var p = document.createElement(“p”); –p.innerHTML = “Some text”; //able to include html tag –p.innerHTML = “Some text ”; –Also the same (drawback – cannot put html tag) Var p = document.createElement(“p”); Var p = document.createElement(“p”); var textNode = document.createTextNode(" Some text"); var textNode = document.createTextNode(" Some text"); p.appendChild(textNode); p.appendChild(textNode);

JS: Document Object Model (DOM) innerHTML – notes Not actually a part of the W3C DOM specification, Not actually a part of the W3C DOM specification, However can provides a simple way to completely replace the contents of an element or textNode and also table cell content However can provides a simple way to completely replace the contents of an element or textNode and also table cell content –document.body.innerHTML = ""; –// Replaces body content with an empty string. Supported both IE and Mozilla/Chrome Supported both IE and Mozilla/Chrome

JS: Document Object Model (DOM) DOM Methods and Properties DOM Methods and Properties –document.getElementById(ID) returns the element with the specified ID attribute. –document.getElementsByTagName(tag) returns an array of the elements with the specified tag name. You can use the asterisk (*) as a wildcard to return an array containing all of the nodes in the document.

JS: Document Object Model (DOM) DOM Methods and Properties DOM Methods and Properties –document.createElement(tag) creates a new element with the specified tag name. –document.createTextNode(text) creates a new text node containing the specified text. –document.documentElement is an object that represents the document itself, and can be used to find information about the document.

JS: DOM – Creating HTML doc. Activity 11:4 Get the body reference Get the body reference –var body = document.getElementsByTagName(“BODY").item(0); Create the tag and complete the tag properties, children… etc Create the tag and complete the tag properties, children… etc –var pElement = document.createElement("p"); –pElement.innerHTML = "Hello Class, Welcome to DOM world! “ –Or var textNode = document.createTextNode("Hello Class, Welcome to DOM world!"); var textNode = document.createTextNode("Hello Class, Welcome to DOM world!"); pElement.appendChild(textNode); pElement.appendChild(textNode); Add the tag to body Add the tag to body –body.appendChild(pElement);

JS: DOM – Adding or modifying HTML element properties Methods Methods –getAttribute(“attribute_name”) Same method: Same method: –getAttributeNode(“attribute_name”) Retrieve the node representation of the named attribute from the current node. Retrieve the node representation of the named attribute from the current node. –setAttribute(“attribute_name”, “attribute_value”) Adds a new attribute or changes the value of an existing attribute on the specified element. Adds a new attribute or changes the value of an existing attribute on the specified element. –hasAttribute((“attribute_name”) Return boolean Return boolean –removeAttribute(“attribute_name”)

JS: DOM – Adding or modifying HTML element properties Activity 11:5 Create the element Create the element –var linkElement = document.createElement("a"); Set the element content Set the element content –linkElement.innerHTML = "Click me to go to google!"; Set element property Set element property –linkElement.setAttribute("href", " To change attribute value: To change attribute value: –Get the element –Use setAttribute with a new value

JS: DOM – Creating HTML table Activity 11:6 Create table Create table –var tbl = document.createElement("table"); Create table body Create table body –var tblBody = document.createElement("tbody"); Create row(tr) and cells(td) Create row(tr) and cells(td) –First create row: var row = document.createElement("tr"); –Then create col: var cell = document.createElement("td"); Create cell content: Create cell content: –var cellText = document.createTextNode("cell content"); –cell.appendChild(cellText); –Add cell (td) to row: row.appendChild(cell); row.appendChild(cell);

JS: DOM – Creating HTML table – cont. Add row(tr) to table body(tbody) Add row(tr) to table body(tbody) –tblBody.appendChild(row); Add table body(tbody) to table(table) Add table body(tbody) to table(table) –tbl.appendChild(tblBody); Creating cell content using innerHTML Creating cell content using innerHTML –var cell = document.createElement("td"); –cell.innerHTML = "cell content";