Douglas Crockford

Blog

Books

Videos

2024 Appearances

JavaScript

Misty

JSLint

JSON

Github

Electric Communities

Mastodon/Layer8

Flickr Photo Album

ResearchGate

LinkedIn

Pronouns: pe/per

About

Plain Old JavaScript and the DOM

JavaScript was specifically invented to add interactivity to web browsers. The two parts of this are accepting input from the human and dynamically altering the display. Before there was JavaScript, the display was altered by doing a page replacement. JavaScript gives us more options.

document.write

We will not discuss this particular mistake here except to mention that it is a security vulnerability waiting for exploitation.

innerHTML

This was an invention of Microsoft. While slightly less reckless that document.write, it is also unsafe and should no longer be used.

The Document Object Model

The DOM is a clumsy API for creating, linking, manipulating, and destroying nodes in the document tree. Twenty years ago I was recommending that it not be used directly, that an intervening library should be used instead. My reasons were that the DOM was poorly designed, poorly implemented, and full of inconsistencies from one brand of browser to the other, and from one version to another. The DOM was a deep source of pain and misery that could be mitigated by a good library.

Since then, the web standards community has, for the most part, corrected many of WC3's failures. The DOM is much less deficient and much more portable and reliable. That is why I now recommend abandoning the libraries, which have grown into bloated platforms, and instead using the DOM and plain old JavaScript together.

A modest example

Somehow, in the course of interacting with the user, we acquire some data that we present as a table:

The DOM is verbose and weird. We make it a little nicer with a wee dom function that makes a node and links text and other nodes to it. Thus armed, we can build the table and link it into a div of output.