Document Object Model. Back to the DOM… Javascript and the DOM  Originally, the Document Object Model (DOM) and Javascript were tightly bound  Each.

Slides:



Advertisements
Similar presentations
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Advertisements

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
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.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Lesson 12- Unit L Programming Web Pages with JavaScript.
Document Object Model. Back to the DOM… Javascript and the DOM  Originally, the Document Object Model (DOM) and Javascript were tightly bound  Each.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic DHTML.
DHTML - Introduction Introduction to DHTML, the DOM, JS review.
The Document Object Model (DOM) & Asynchronous Javascript And XML (AJAX) : an introduction UFCEKG-20-2 : Data, Schemas and Applications.
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)
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
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.
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.
1 JavaScript: Objects and Object Models October 25, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel,
Working with Objects Creating a Dynamic Web Page.
CITS1231 Web Technologies JavaScript and Document Object Model.
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.
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.
DHTML - Introduction Chapter Introduction to DHTML, the DOM, JS review.
Interactive Client-Side Technologies MMIS 656 Web Design Technologies Acknowledgements: Estrella, S. (2003). The Web Wizard’s Guide to DHTML and CSS.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 20 – Dynamic HTML: Object Model and Collections Outline 20.1Introduction 20.2Object Referencing.
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.
JavaScript - A Web Script Language Fred Durao
Chapter 8 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 8 Sebesta: Programming the World Wide Web.
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.
Document Objects Forms, Images and Links. The document object model Each element of an HTML document, such as an image, form, link, or button, can be.
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.
CSS1 Dynamic HTML Objects, Collections & Events. CSS2 Introduction Dynamic HTML treats HTML elements as objects. Element’s parameters can be treated as.
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.
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.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
The Web Wizard’s Guide To DHTML and CSS Chapter 2 A Review of CSS2 and JavaScript.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Dynamic HTML: Object Model and Collections Outline 15.1Introduction 15.2Object Referencing.
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.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
THE DOM.
DHTML.
Applied Component I Unit II Introduction of java-script
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
DOM Document Object Model.
Chapter 13 - Dynamic HTML: Object Model and Collections
Working with Dynamic Content and Styles
Document Object Model (DOM): Objects and Collections
Document Object Model.
Introduction to DHTML, the DOM, JS review
Presentation transcript:

Document Object Model

Back to the DOM…

Javascript and the DOM  Originally, the Document Object Model (DOM) and Javascript were tightly bound  Each major browser line (IE and Netscape) had their own overlapping DOM implementation  There's also some jargon issues, eg. DHTML…  Now, the DOM is a separate standard, and can be manipulated by other languages (eg Java, server side javascript, python, etc)  Browsers still differ in what parts of the standard they support, but things are much better now  Originally, the Document Object Model (DOM) and Javascript were tightly bound  Each major browser line (IE and Netscape) had their own overlapping DOM implementation  There's also some jargon issues, eg. DHTML…  Now, the DOM is a separate standard, and can be manipulated by other languages (eg Java, server side javascript, python, etc)  Browsers still differ in what parts of the standard they support, but things are much better now

Review: DOM Structure  Objects are in a hierarchy  The window is the parent for a given web page  Document is the child with the objects that are most commonly manipulated  Objects are in a hierarchy  The window is the parent for a given web page  Document is the child with the objects that are most commonly manipulated window * location * frames * history * navigator * event * screen * document o links o anchors o images o filters o forms o applets o embeds o plug-ins o frames o scripts o all o selection o stylesheets o body table from:

DOM Tree  The usual parent/child relationship between node  Like any other tree, you can walk this  The usual parent/child relationship between node  Like any other tree, you can walk this diagram from

Referencing Objects  Objects can be referenced  by their id or name (this is the easiest way, but you need to make sure a name is unique in the hierarchy)  by their numerical position in the hierarchy, by walking the array that contains them  by their relation to parent, child, or sibling (parentNode, previousSibling, nextSibling, firstChild, lastChild or the childNodes array  Objects can be referenced  by their id or name (this is the easiest way, but you need to make sure a name is unique in the hierarchy)  by their numerical position in the hierarchy, by walking the array that contains them  by their relation to parent, child, or sibling (parentNode, previousSibling, nextSibling, firstChild, lastChild or the childNodes array

A div example  the div is an element with an id of mydiv  It contains a text element, which can be referenced by childNodes[0] (childNode being an array of all childen of a node  So the text in the div is not a value of the div, but rather the value of the first (and only) childNode of the div  the div is an element with an id of mydiv  It contains a text element, which can be referenced by childNodes[0] (childNode being an array of all childen of a node  So the text in the div is not a value of the div, but rather the value of the first (and only) childNode of the div This is some simple html to display </div>

Zen Garden Example 1  A loop of code to list the links…. for (var i = 0; i < document.links.length; i++) { document.write(" Link number " + i + " has these properties: "); document.write(" Link number " + i + " has these properties: "); document.write(" nodeName is " + document.links[i].nodeName + " "); document.write(" nodeName is " + document.links[i].nodeName + " "); document.write(" nodeType is " + document.links[i].nodeType + " "); document.write(" nodeType is " + document.links[i].nodeType + " "); document.write(" parentNode.nodeValue is " + document.links[i].parentNode.nodeValue + " "); document.write(" parentNode.nodeValue is " + document.links[i].parentNode.nodeValue + " "); document.write(" firstChild is " + document.links[i].firstChild + " "); document.write(" firstChild is " + document.links[i].firstChild + " "); document.write(" firstChild.nodeValue is " + document.links[i].firstChild.nodeValue + " "); document.write(" firstChild.nodeValue is " + document.links[i].firstChild.nodeValue + " "); document.write(" href is " + document.links[i].href + " "); document.write(" href is " + document.links[i].href + " "); }

Zen Garden Example 2  Same as example one, but with another loop to look for all span tags….

Zen Garden Example 2  I added a little javascript to the sample file from zen garden  This will search for a given tag using the getElementsByTagName() method  The result of this method is an array, and we can walk that array and then write out different properties and values for the elements found by getElementsByTagName()  There's also a getElementsById() method  I added a little javascript to the sample file from zen garden  This will search for a given tag using the getElementsByTagName() method  The result of this method is an array, and we can walk that array and then write out different properties and values for the elements found by getElementsByTagName()  There's also a getElementsById() method

Zen Garden Example 2 var look_for="span"; document.write(" Looking for " + look_for + " tags "); var x=document.getElementsByTagName(look_for); for (var i = 0; i < x.length; i++) { document.write(" Tag " + look_for + " number " + i + " has these properties: "); document.write(" Tag " + look_for + " number " + i + " has these properties: "); document.write(" nodeName is " + x[i].nodeName + " "); document.write(" nodeName is " + x[i].nodeName + " "); document.write(" nodeValue is " + x[i].nodeValue + " "); document.write(" nodeValue is " + x[i].nodeValue + " "); document.write(" nodeType is " + x[i].nodeType + " "); document.write(" nodeType is " + x[i].nodeType + " "); document.write(" id is " + x[i].id + " "); document.write(" id is " + x[i].id + " "); document.write(" name is " + x[i].name + " "); document.write(" name is " + x[i].name + " "); document.write(" parentNode is " + x[i].parentNode + " "); document.write(" parentNode is " + x[i].parentNode + " "); document.write(" parentNode.nodeValue is " + x[i].parentNode.nodeValue + " "); document.write(" parentNode.nodeValue is " + x[i].parentNode.nodeValue + " "); document.write(" firstChild is " + x[i].firstChild + " "); document.write(" firstChild is " + x[i].firstChild + " "); document.write(" firstChild.nodeValue is " + x[i].firstChild.nodeValue + " "); document.write(" firstChild.nodeValue is " + x[i].firstChild.nodeValue + " "); }

Manipulating Objects  As said, it's easiest to reference objects by id  To do this easily, use getElementById(), which returns the element with the given id  For example, if you want to find a div with the id of "my_cool_div", use getElementById("my_cool_div")  Keep in mind that it's the element itself that's returned, not any particular property  As said, it's easiest to reference objects by id  To do this easily, use getElementById(), which returns the element with the given id  For example, if you want to find a div with the id of "my_cool_div", use getElementById("my_cool_div")  Keep in mind that it's the element itself that's returned, not any particular property

Using divs  Div properties can be dynamically manipulated  You can use this to make menus more dynamic  Div properties can be dynamically manipulated  You can use this to make menus more dynamic

colors: The first version  The basic div: colors01.html <div id="item1" class="content" onMouseOver="changeColor('item1', '#fdd');" onMouseOver="changeColor('item1', '#fdd');" onMouseOut="changeColor('item1', '#cff');"> onMouseOut="changeColor('item1', '#cff');"> UNC UNC </div><br>

colors: The First Version  And a function (notice the alert): <script> function changeColor(item, color) { document.getElementById(item).style.backgroundColor document.getElementById(item).style.backgroundColor =color; =color; //alert(document.getElementById(item).childNodes[1]); //alert(document.getElementById(item).childNodes[1]); document.getElementById(item).childNodes[1].style.backgroundColor document.getElementById(item).childNodes[1].style.backgroundColor =color; =color; }</script> colors01.html

Version 2  The div structure, sans link: colors02.html <div id="item1" class="content" onMouseOver="changeColor('item1', change_color);" onMouseOver="changeColor('item1', change_color);" onMouseOut="changeColor('item1', start_color);" onMouseOut="changeColor('item1', start_color);" onClick="document.location=' onClick="document.location=' > UNC UNC</div>

Version 2  And the function, with some vars colors02.html <script> function changeColor(item, color) { document.getElementById(item).style.backgroundColor=color; document.getElementById(item).style.backgroundColor=color; } var start_color="#cff"; var change_color="#fdd"; </script>

Version2  Much cleaner  div clickable means no issues with color of link, but sacrifices visited link color (how could that be fixed?)  Use of variables for colors mean it's much easier to change them (but this could be made much easier with php in the background…)  Much cleaner  div clickable means no issues with color of link, but sacrifices visited link color (how could that be fixed?)  Use of variables for colors mean it's much easier to change them (but this could be made much easier with php in the background…)

innerHTML  innerHTML is a property of any document element that contains all of the html source and text within that element  This is not a standard property, but widely supported--it's the old school way to manipulate web pages  Much easier than building actual dom subtrees, so it's a good place to start  Very important--innerHTML treats everything as a string, not as DOM objects (that's one reason it's not part of the DOM standard)  innerHTML is a property of any document element that contains all of the html source and text within that element  This is not a standard property, but widely supported--it's the old school way to manipulate web pages  Much easier than building actual dom subtrees, so it's a good place to start  Very important--innerHTML treats everything as a string, not as DOM objects (that's one reason it's not part of the DOM standard)

Using These….  You can reference any named element with getElementById()  You can read from or write to that element with innerHTML  For example: getElementById("mydiv").innerHTML ="new text string";  You can reference any named element with getElementById()  You can read from or write to that element with innerHTML  For example: getElementById("mydiv").innerHTML ="new text string";

A Simple DOM example <p> This some simple html to display </p></div> <input type="button" value="Alert innerHTML of mydiv" onclick=" onclick=" alert(getElementById('mydiv').innerHTML) alert(getElementById('mydiv').innerHTML) " /> " /></form> simple_dom_example.html

Manipulating Visibility  You can manipulate the visibility of objects, this from  The first part displays an element if it's hidden…  You can manipulate the visibility of objects, this from  The first part displays an element if it's hidden… 31_dhtml_example.html function changeDisplayState (id) { trigger=document.getElementById("showhide"); trigger=document.getElementById("showhide"); target_element=document.getElementById(id); target_element=document.getElementById(id); if (target_element.style.display == 'none' if (target_element.style.display == 'none' || target_element.style.display == "") || target_element.style.display == "") { target_element.style.display = 'block'; target_element.style.display = 'block'; trigger.innerHTML = 'Hide example'; trigger.innerHTML = 'Hide example'; }…

Manipulating Visibility  And the second hides the same element if it's visible 31_dhtml_example.html … else else { target_element.style.display = 'none'; target_element.style.display = 'none'; trigger.innerHTML = 'Show example'; trigger.innerHTML = 'Show example'; } }