Site Navigation

Tuesday, October 16, 2007

What If? - DOM Method Improvements

What If?

"Missing Methods" in the ECMAScript specification.

Having a complete specification for ECMAScript's DOM manipulation is great, however in time (as with any API) you yearn for some additional features.

IMHO, these methods would be great additions to the current spec.


Object Element
getElementsByTagName(name, depth)
This method returns a NodeList.
The name parameter is of type DOMString.
The depth parameter is of type unsigned long.

With the optional depth parameter, developers can request what level of nesting they specifically wish to return.
If omitted, zero or negative, the default behavior of all matching child nodes is returned.

If requested, only elements at the requested level would be returned.

var allChildCells = myTable.getElementsByTagName('td');
var directCells = myTable.getElementsByTagName('td', 1);
var grandChildCells = myTable.getElementsByTagName('td', 2);

It is already on the way in HTML 5 (Web Applications 1.0)!
Better yet, it is real! Firefox 3 already has it!
And so does Opera 9.5!
And so does WebKit!

Object Node
This method returns a Node.
The newChild parameter is of type Node.
The refChild parameter is of type Node.

Similar to the insertBefore method, except the newChild is inserted
after the refChild in the DOM tree.

Object NodeList
This method returns a NodeList.
The index parameter is of type unsigned long.

When a NodeList is returned from a method like .getElementsByTagName(name) it is
often desired to "filter" out undesired items.
Currently this is only possible by "casting" to an Array (a.k.a. iterate over the
NodeList and copy to an Array)

Ok, so not exactly a browser bug... but some interesting ideas.

What do you think? What methods would you add/modify?

Have your say!