Chapter 2. Language and Browser Utilities

This chapter formally introduces the language utilities that you'll find in Base. The language utilities are designed to streamline some of the most commonly worked-around problems in JavaScript programming, and they're designed to be ultra-portable and highly optimized. Regardless of whether you use anything else in the entire toolkit, the constructs presented in this chapter are worth a hard look because they provide augmentation that is difficult to imagine living without once you've gotten used to using them. Manipulating arrays, cloning nodes, adding and removing classes, and calculating margin and content boxes for DOM nodes are among the topics included in this chapter.

Looking Up DOM Nodes

The previous chapter introduced dojo.byId, a toolkit-specific mechanism for looking up DOM nodes in a manner that is more portable and predictable than document.getElementById. Although dojo.byId absolutely pervades Dojo development, there is little value in repeating the previous discussion from Chapter 1; refer back to the previous chapter for a detailed outline involving some of the issues with document.getElementById and how dojo.byId alleviates them. As a reminder, though, here's the full API call for dojo.byId :

dojo.byId(/*String*/ id | /*DomNode*/ node, /*DomNode*/doc)  // Returns a DOM Node

Example 2-1 lists some other common use patterns.

Example 2-1. Quick reminder about dojo.byId

var foo = dojo.byId("foo"); //returns the node with id=foo if one exists
dojo.byId(foo).innerHTML="bar"; //the lookup is a no-op since foo is
                         //a node; then sets innerHTML to "bar"
var bar = dojo.byId("bar", baz); //returns the node with id=bar in document
                         //referenced by baz if one exists

Get Dojo: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.