O'Reilly logo

YUI 3 Cookbook by Evan Goer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 2. DOM Manipulation

The document object model (DOM) is not a particularly pleasant API to program against.

The main reason for this is that historically, browser DOM implementations have been incredibly buggy and inconsistent. Although JavaScript itself has its share of design flaws, many common complaints about JavaScript are actually complaints about the DOM.

A perhaps less appreciated reason is that the DOM is a low-level API that exposes only basic capabilities. By design, low-level APIs avoid making too many assumptions about how developers might want to use the underlying objects. Certain popular DOM extensions such as innerHTML and querySelector could be considered more mid-level, as they evolved based on what developers were actually doing.

JavaScript libraries have the advantage of being free to provide higher-level APIs that are more intuitive and terse than the lower-level DOM. However, each library comes with a strong mental model for how to work with the DOM. It would be a mistake to bake those models deeply into the DOM itself. (Imagine how unhappy jQuery developers would be if the only way to work with the DOM was the YUI way, or vice versa.)

In any case, the rise of JavaScript libraries has made it far easier to manipulate the DOM. A good DOM abstraction layer can:

  • Correct for bugs and implementation differences in specific browsers. YUI accomplishes this using feature detection (testing for the existence of a feature) and capability detection (verifying whether ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required