JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.21

A Web Document as A Tree of Nodes

This section provides a tutorial example of JavaScript to show that a Web page is a tree of nodes. This shows the simplified flattened view of DOM API.

We know that a Web page, a HTML document, is a tree of elements (ignoring attributes for a moment). From a simplified point of view, all elements are represented by the "Node" interface. To differentiate different types of nodes from each other, we can check the "type" and "name" attributes.

Now let's walk through the tree and present each element as a node with its type and name with a simple JavaScript example:

<html>
<!-- DOM_API_Simplified_Node_View.html
   Copyright (c) 2013 by Dr. Herong Yang, herongyang.com
-->
<head>
<title>DOM API - Simplified Node View</title>
<style type="text/css">body {background-color: #aaaaff}</style>
<script type="text/javascript">
function showInterface(node, prefix, out) {
   var typeName = new Array("NULL", "ELEMENT", "ATTRIBUTE", "TEXT",
      "CDATA", "ENTITY_REFERENCE", "ENTITY", "RROCESSING_INSTRUCTION",
      "COMMENT", "DOCUMENT", "DOCUMENT_TYPE", "DOCUMENT_FRAGMENT",
      "NOTATION");
   out += prefix+"[Node type="+typeName[node.nodeType]
      +" name="+node.nodeName+"]\n";
   var list = node.childNodes;
   for (var i=0; i<list.length; i++) {
      out = showInterface(list.item(i), prefix+"   ", out);
   }
   return out;
}
function show() {
   var text = document.createTextNode(showInterface(document,"",""));
   var display = document.getElementById("display");
   display.replaceChild(text,display.firstChild);
}
</script>
</head>
<body>
This first text is in the "body" element directly.
<p>This second text is in a "p" element.</p>
<form action="nothing" method="get">
   <input type="button" value="View" onClick="show();"/>
</form>
<pre id="display">
Click the View button to see interface names...
</pre>
</body>
</html>

Notice that I am using an array to translate "node.nodeType" from an integer to a string.

In you run this JavaScript page in Firefox 2.0 and click the "View" button, you will get a nice tree list of nodes with their types and names used in this Web page:

[Node type=DOCUMENT name=#document]
   [Node type=ELEMENT name=HTML]
      [Node type=ELEMENT name=HEAD]
         [Node type=COMMENT name=#comment]
         [Node type=ELEMENT name=TITLE]
            [Node type=TEXT name=#text]
         [Node type=TEXT name=#text]
         [Node type=ELEMENT name=STYLE]
            [Node type=TEXT name=#text]
         [Node type=TEXT name=#text]
         [Node type=ELEMENT name=SCRIPT]
            [Node type=TEXT name=#text]
      [Node type=ELEMENT name=BODY]
         [Node type=TEXT name=#text]
         [Node type=ELEMENT name=P]
            [Node type=TEXT name=#text]
         [Node type=TEXT name=#text]
         [Node type=ELEMENT name=FORM]
            [Node type=TEXT name=#text]
            [Node type=ELEMENT name=INPUT]
            [Node type=TEXT name=#text]
         [Node type=TEXT name=#text]
         [Node type=ELEMENT name=PRE]
            [Node type=TEXT name=#text]
         [Node type=TEXT name=#text]

Table of Contents

 About This JavaScript Tutorial Example Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

W3C's Document Object Model (DOM) Specifications

 Overview of DOM Specifications

 DOM Level 0 - Example

 DOM Level 1 - Example

 DOM Level 2 - Example

 DOM Level 3 - Example

 DOM Level Test - document.implementation.hasFeature

 Inheritance vs. Flattened Views of the API

 A Web Document as A Tree of Different Interfaces

A Web Document as A Tree of Nodes

 Dump Document in a New Window - JavaScript Source

 Dump Document in a New Window - Firefox 2.0 Result

 Dump Document in a New Window - IE 6.0 Result

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2013
A Web Document as A Tree of Nodes