A Web Document as A Tree of Different Interfaces

This section provides a tutorial example of JavaScript to show different DOM interfaces for different elements used in a Web page. This shows the object oriented view of DOM API.

We know that a Web page, a HTML document, is a tree of elements (ignoring attributes for a moment). From an object oriented point of view, different elements are represented by different types of objects, or interfaces in DOM term.

Now let's walk through the tree and figure out which DOM interface is supported on each element with a simple JavaScript example:

<html>
<!-- DOM_API_Object_Oriented_View.html
   Copyright (c) 2013 by Dr. Herong Yang, herongyang.com
-->
<head>
<title>DOM API - Object Oriented View</title>
<style type="text/css">body {background-color: #aaaaff}</style>
<script type="text/javascript">
function showInterface(node, prefix, out) {
   out += prefix+node.constructor+"\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 a special property "node.constructor" to get the name of the constructor function name, which is also the name of the JavaScript class that implements the DOM interface. "node.constructor" is not valid DOM attribute.

I have tried to use the DOM attribute "node.className" to get the implementation class name. But Firefox returns "undefined".

In you run this JavaScript page in Firefox 18 and click the "View" button, you will get a nice tree list of DOM interfaces used in this Web page:

[object HTMLDocument]
   [object HTMLHtmlElement]
      [object Comment]
      [object HTMLHeadElement]
         [object Text]
         [object HTMLTitleElement]
            [object Text]
         [object Text]
         [object HTMLStyleElement]
            [object Text]
         [object Text]
         [object HTMLScriptElement]
            [object Text]
         [object Text]
      [object Text]
      [object HTMLBodyElement]
         [object Text]
         [object HTMLParagraphElement]
            [object Text]
         [object Text]
         [object HTMLFormElement]
            [object Text]
            [object HTMLInputElement]
            [object Text]
         [object Text]
         [object HTMLPreElement]
            [object Text]
         [object Text]

Table of Contents

 About This 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