The invention of JavaScript in the 1990s signified a major turning point for the World Wide Web. Pre­vi­ous­ly static websites now developed into ever more dynamic web projects – thanks to the cor­re­spond­ing elements in the renowned script language. Browser makers reacted with the im­ple­men­ta­tion of a suitable in­ter­preter and the design of their own model for dynamic HTML (DHTML). Only through these in­ter­preters and models were com­pre­hen­sive struc­tur­al and optical changes in the web document possible during the readout in the browser, which enabled the ex­ploita­tion of JavaScript’s full potential.

Because the different tech­niques were in no way geared towards each other, web de­vel­op­ers combined them to offer their dynamic projects to all browsers. However, this required con­sid­er­able effort. For this reason, the W3C Con­sor­tium released the first Document Object Models (DOM) spec­i­fi­ca­tion in 1998, which continues to play a central role in JavaScript pro­gram­ming to date.

What is the Document Object Model (DOM)?

The Document Object Model, DOM for short, is a stan­dard­ized pro­gram­ming interface for the struc­tur­ing of HTML and XML documents. It was developed and released by the World Wide Con­sor­tium (W3C), the or­ga­ni­za­tion founded by web inventor Tim Berners-Lee in 1994, to design and establish standards for the World Wide Web.

The purpose of the Document Object Model is to make it as easy as possible for pro­gram­mers to access a web project’s com­po­nents so that they can add to, delete and edit the project’s content, at­trib­ut­es, and styles. DOM serves as a platform-in­de­pen­dent and language-neutral link between script languages such as JavaScript and the basic web document, as it depicts the structure of the document in a tree structure in which every node is an in­de­pen­dent, se­lec­table object. Because of this structure, one also refers to this type of displayed web project version as a DOM tree.

Note

Different from what the name suggests, DOM is not actually a model. It is rather – as already mentioned –a pro­gram­ming interface. However, a Document Object Model can be seen in a fig­u­ra­tive sense as a model for how to access the web data that’s being depicted as an object.

Where and when are DOM trees used?

The Document Object Model was developed for use across the World Wide Web, which is where it is primarily in use. More specif­i­cal­ly, it is the par­tic­u­lar browsers that make use of stan­dard­ized in­ter­faces.

In this way, popular web clients use DOM or DOM-based in­ter­faces in order to render accessed HTML or XML pages. During this process, the in­di­vid­ual com­po­nents are combined together as nodes and organized in an in­di­vid­ual DOM tree. At the same time, the re­spec­tive browser loads this rendered version of the web document into the local storage in order to analyze or process it, and finally to be able to present the page in the form provided by the developer. For the rendering, the browsers rely on different engines (rendering software) such as Gecko (Firefox), WebKit (Safari) or Blink (Chrome, Edge, Opera), which are also based on the DOM standard.

As an object-oriented pre­sen­ta­tion of a web document, the Document Object Model remains relevant after the output – as an interface for all pro­grammed dynamic content, and, con­se­quent­ly, for all user in­ter­ac­tions that can change the site’s ap­pear­ance during the output.

Note

In 2012, the W3C Con­sor­tium in­tro­duced a special interface with the name Shadow DOM as one of the four pillars of the so-called web com­po­nents. This makes it possible to enlarge the actual Document Object Model of a web document through in­de­pen­dent subtrees (shadow trees). You can find out more on the topic of Shadow DOM.

How is the Document Object Model struc­tured?

HTML markups define re­la­tion­ships between the different tags. In this way, web document elements iden­ti­fied by tags are su­per­or­di­nate or sub­or­di­nate, for example, depending on their role within the web project. Fur­ther­more, some tags can be contained within others. In order to simply display these hi­er­ar­chies ad­e­quate­ly in the Document Object Model, the interface makes recourse of the tree structure mentioned above, which makes it possible to arrange the rendered objects ac­cord­ing­ly.

How a DOM tree is struc­tured thus always depends on the un­der­ly­ing HTML or XML document. For the former, however, the following cross-project basic hierarchy can be adhered to:

As in the HTML basic structure, the HTML object itself is located at the top position of the hierarchy. Sub­or­di­nat­ed beneath it are the header and the body of the website. With the latter, it must be assumed that it contains at least one paragraph (section with text content).

Note

The in­di­vid­ual splits in the DOM tree are called nodes. In addition, one must dis­tin­guish between element nodes such as the HTML, body, header and paragraph objects that can be seen above, attribute nodes such as “align” or “size,” and text nodes.

Document Object Model: a practical example (includes HTML code)

Now that we have carefully examined the function and the general structure of the Document Object Models in the previous sections, we should conclude by il­lus­trat­ing the basic struc­tur­ing technique for web script languages using a specific example. The following simple HTML document with header (incl. linking to the CSS stylesheet) and body serves as a basis for this, as well as two content elements (image and text):

<html>
    <head>
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <link href="style.css" rel="stylesheet">
        <title>Title</title>
    </head>
    <body>
        <p><strong>Hello World!</strong></p>
        <div><img src="bild.jpg"></div>
    </body>
</html>

The DOM tree generated from this HTML code looks like this:

Al­ter­na­tive­ly, you’ll fre­quent­ly find the following pre­sen­ta­tion form for the Document Object Model example utilized here:

DOCTYPE: HTML
HTML
----head
-  ---- meta
-  ---- link
-  ---- title
----body
-  ---- p
-    ---- strong
-      ---- TEXT: Hello World!
-  ---- div
-    ---- img
Go to Main Menu