Document Object Model (DOM)

Slides:



Advertisements
Similar presentations
23-Aug-14 HTML/XHTML Forms. 2 What are forms? is just another kind of XHTML/HTML tag Forms are used to create (rather primitive) GUIs on Web pages Usually.
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.
1 The Information School of the University of Washington Nov 1fit dom © 2006 University of Washington Document Object Model (DOM) INFO/CSE 100, Fall.
1 The Information School of the University of Washington Oct 30fit review Programming Review INFO/CSE 100, Fall 2006 Fluency in Information Technology.
1 The Information School of the University of Washington Nov 8fit review © 2006 University of Washington Midterm 2 Review INFO/CSE 100, Fall 2006.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Python and Web Programming
Oct 25 1 The Information School of the University of Washington fit control © 2006 University of Washington Control Flow INFO/CSE 100, Fall 2006.
4.1 JavaScript Introduction
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
1 Essential HTML coding By Fadi Safieddine (Week 2)
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
CITS1231 Web Technologies JavaScript and Document Object Model.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
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.
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.
XML DOM Week 11 Web site:
Net-centric Computing Document Object Model (DOM).
Introduction to Web Site Development Department of Computer Science California State University, Los Angeles Lecture 9: JavaScript.
Tarik Booker CS 120 California State University, Los Angeles.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 The Document Object Model. 2 Objectives You will be able to: Describe the structure of the W3C Document Object Model, or DOM. Use JavaScript to access.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
HTML Structure & syntax
HTML Structure & syntax
XHTML Forms.
Introduction to.
DHTML.
Javascript and Dynamic Web Pages: Client Side Processing
Programming Web Pages with JavaScript
Applied Component I Unit II Introduction of java-script
>> JavaScript: Document Object Model
Just enough to get going!
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Internet of the Past.
Introduction to HTML.
HTML & teh internets.
Introduction to HTML.
A little MORE on JavaScript
The Document Object Model (DOM) is a W3C standard.
Section 17.1 Section 17.2 Add an audio file using HTML
Document Object Model (DOM)
Document Object Model (DOM)
Scripting Languages and the DOM
Document Object Model (DOM): Objects and Collections
Revision.
DHTML.
JavaScript Introduction
Week 11 Web site: XML DOM Week 11 Web site:
Document Object Model (DOM)
DHTML Javascript Internet Technology.
A second look at JavaScript
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Document Object Model (DOM)
DHTML Javascript Internet Technology.
CHAPTER 7 JavaScripts & HTML Documents
INFO/CSE 100, Spring 2006 Fluency in Information Technology
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Introduction to HTML5.
Teaching slides Chapter 6.
INFO/CSE 100, Spring 2005 Fluency in Information Technology
The Most Basic HTML Page
Introduction to Programming and JavaScript
HTML Structure & syntax
Web Programming and Design
Web Programming and Design
Presentation transcript:

Document Object Model (DOM) Nov 1 fit100-16-dom © 2006 University of Washington

fit100-16-dom © 2006 University of Washington References References JavaScript, The Definitive Guide by David Flanagan. Publisher O'Reilly W3C Document Object Model http://www.w3.org/DOM/ http://www.w3.org/2003/02/06-dom-support.html Document Object Model in Mozilla http://www.mozilla.org/docs/dom/ Nov 1 fit100-16-dom © 2006 University of Washington

fit100-16-dom © 2006 University of Washington What the heck is the DOM? Document Object Model Your web browser builds a model of the web page (the document) that includes all the objects in the page (tags, text, etc) All of the properties, methods, and events available to the web developer for manipulating and creating web pages are organized into objects Those objects are accessible via scripting languages in modern web browsers Mental model of a car. When you rent a car that you have never driven before, do you freak out about it? No! You have a model of how to drive it in your mind. This is called a mental model. Your web browser builds an actual model of a web page... Nov 1 fit100-16-dom © 2006 University of Washington

This is what the browser reads <html> <head> <title>Sample DOM Document</title> </head> <body> <h1>An HTML Document</h1> <p>This is a <i>simple</i> document. </body> </html> This is what the browser displays on screen.

Document This is a drawing of the model that the browser is working with for the page. <html> <head> <body> <title> "Sample DOM Document" <h1> <p> "An HTML Document" "This is a" <i> "document" "simple"

But the HTML you write is parsed by the browser and turned into the DOM.

View Source just shows you the HTML that makes up that page View Source just shows you the HTML that makes up that page. It's probably the exact HTML that you wrote. It might look like different code if, for example, you work in template files in a backend language and don't look at the compiled HTML output very often. Or there is a "build process" that happens after you write your HTML and the code is put out to the live website. Perhaps that HTML is compressed or otherwise changed. View Source is a little weird actually. The only people that would care to look at that code are developers and all the major browsers have built in developer tools now. It has probably out-lived its usefulness.

When you're looking at the panel in whatever DevTools you are using that shows you stuff that looks like HTML, that is a visual representation of the DOM! We made it!

Well, yeah, it does. It was created directly from your HTML remember Well, yeah, it does. It was created directly from your HTML remember. In most simple cases, the visual representation of the DOM will be just like your simple HTML. But it's often not the same...

fit100-16-dom © 2006 University of Washington Why is this useful? Because we can access the model too! the model is made available to scripts running in the browser, not just the browser itself A script can find things out about the state of the page A script can change things in response to events, including user requests We have already used this capability in the GUI programming that we've done Nov 1 fit100-16-dom © 2006 University of Washington

Recall our simple GUI example This GUI has several simple controls. Two buttons to control the results One text field to display the results One pair of radio buttons to control the display One button to reinitialize Nov 1 fit100-16-dom © 2006 University of Washington

setResults(resultString) <script type="text/javascript"> function setResults(resultString) { var tempString = resultString; if (document.getElementById("radioLC").checked) { tempString = tempString.toLowerCase(); } else if (document.getElementById("radioUC").checked) { tempString = tempString.toUpperCase(); } document.getElementById("resultField").value = tempString; </script> the highlighted script above makes reference to several objects in the document object model Nov 1 fit100-16-dom © 2006 University of Washington

document.getElementById("radioLC").checked Reference to several nodes in the model of the page that the browser constructed document The root of the tree is an object of type HTMLDocument Using the global variable document, we can access all the nodes in the tree, as well as useful functions and other global information title, referrer, domain, URL, body, images, links, forms, ... open, write, close, getElementById, ... Nov 1 fit100-16-dom © 2006 University of Washington

Some information from a document <html> <head> <title>DOM Sample 1</title> </head> <body> Information about this document.<br> <script type="text/javascript"> document.write("<br>Title: ",document.title); document.write("<br>Referrer: ",document.referrer); document.write("<br>Domain: ",document.domain); document.write("<br>URL: ",document.URL); </script> </body> </html> Show dom1.html

fit100-16-dom © 2006 University of Washington Nov 1 fit100-16-dom © 2006 University of Washington

document.getElementById("radioLC").checked This is a predefined function that makes use of the id that can be defined for any element in the page An id must be unique in the page, so only one element is ever returned by this function The argument to getElementById specifies which element is being requested Nov 1 fit100-16-dom © 2006 University of Washington

Some information about elements <html> <head> <title>DOM Sample B</title> <script type="text/javascript"> function showInfo() { var element = document.getElementById("opener"); var buffer = element.id + " tag is " + element.tagName; alert(buffer); element = document.getElementById("actionItem"); buffer = element.id + " tag is " + element.tagName; buffer += ", type is "+element.type; } </script> </head> <body> <p id="opener">The id attribute is very helpful.</p> <p id="closer">This is the closing paragraph.</p> <form> <button id="actionItem" type="button" onclick="showInfo()">Show Info</button> </form> </body> </html> Note Element doesn't refer to the value! Just the element. We have to explicitly ask for the value or the attribute values (see above).

Show demo DOM-SampleB.html

document.getElementById("radioLC").checked This is a particular property of the node we are looking at, in this case, a radio button Each type of node has its own set of properties for radio button: checked, name, ... refer to the HTML DOM for specifics for each element type Some properties can be both read and set Nov 1 fit100-16-dom © 2006 University of Washington

Some specific properties <head> <title>Simple Sample GUI</title> <script type="text/javascript"> function setResults(resultString) { var tempString = resultString; if (document.getElementById("radioLC").checked) { tempString = tempString.toLowerCase(); } else if (document.getElementById("radioUC").checked) { tempString = tempString.toUpperCase(); } document.getElementById("resultField").value = tempString; </script> </head>

fit100-16-dom © 2006 University of Washington Getting vs. Setting var oldvalue = document.getElementById("resultField").value; document.getElementById("resultField").value = "new value"; Nov 1 fit100-16-dom © 2006 University of Washington

fit100-16-dom © 2006 University of Washington Just the tip of the DOM The HTML Document Object Model is a standard for structuring data on a web page The field is advancing rapidly as people recognize the benefits of standardized structure and access The DOM is steadily improving to cover general purpose data structuring requirements XML (Extendible Markup Language) also uses the Core DOM to specify its structured data similar to HTML but more carefully defined Nov 1 fit100-16-dom © 2006 University of Washington

This is what the browser reads (dom3.html). <head> <title>DOM Sample 3</title> <script type="text/javascript"> var switchCount = 0; var adjectives = ["simple","complex","fascinating","unique"]; function switcher() { if (switchCount == (adjectives.length - 1)) switchCount = 0; else switchCount++; var italicNode = document.getElementById("adjPhrase"); italicNode.firstChild.nodeValue = adjectives[switchCount]; } </script> </head> <body> <h1>An HTML Document</h1> <p>This is a <i id="adjPhrase">simple</i> document. <form> <button type="button" onclick="switcher()">switch</button> </form> </body> </html>

This is what the browser displays on screen. Show demo dom3.html This is what the browser displays on screen.

fit100-16-dom © 2006 University of Washington CONCLUSION ABOUT DOM DOM is actually a representation in hierarchical way of your web coding It somehow how you documented all the code into some neatly tree. Nov 1 fit100-16-dom © 2006 University of Washington