Christian Heilmann

Training new developers in the valley – Day 3

Sunday, July 27th, 2008 at 3:08 am

On the third day we went deeper into the oddities of the DOM and how to access and create content in the current document. One thing I realized very fast is that teaching DOM before the days of FireBug was much easier – you can lead the group from property to property and method to method. With FireBug they are much faster in finding out what can be done and also get a lot of goodies that FireFox provides but aren’t the standard.

We went through the basics – setAttribute and the differences when using it in comparison to the shorter property notation (MSIE sees expando properties as attributes and in order to remove them you’d have to null both the attribute and the object property).

We then moved on quickly to createElement and createTextNode and detected the need to apply them to the document somehow to make them appear.
This lead to insertBefore and appendChild and we discovered that there is no insertAfter, which is a logical fault in the DOM.
As a remedy I asked the group to write their own insertAfter, which is a good exercise to re-iterate looping through child nodes as well as using the creation methods. There are of course several methods of writing an insertAfter, but I was pretty much stunned to see one of the attendees to come up with one I hadn’t thought of:

function insertAfter(newElm,elm){
var clone = elm.cloneNode(true);

I am not too sure about its performance, but I really like the logic of it: this way you can be sure the new node will be after the old one regardless of where the old node is (last node, first or somewhere in the middle). This also means you don’t need to fork and use insertBefore or appendChild respectively.

Other examples we went through were removing nodes with a certain class (to show the problem of the changing length when iterating over a nodeList and removing elements) and writing a simple form validation script that changes the labels of mandatory fields when they are empty.

I wrapped up the day using the JSON output of the API to write out a list of bookmarks and tags:

In an extra step I then asked the team that instead of calling the API in an own script tag to progressively enhance a link and create the script tag dynamically:

My Delicious Links

We then ranted a bit about the non-logic of DOM methods and their parameter order (“why is document.insertBefore(oldNode,newNode) not possible but instead we need oldNode.parentNode.insertBefore(newNode,oldNode)??”) and came up with a wishlist of DOM methods that should be native:

  • createLink(url,text)
  • insertAfter(newNode,oldNode) – consistent with the native DOM inconsistency
  • removeNode(node)
  • textElement(elementName,text)
  • addScript(url)
  • normalizeNode(node) – removing whitespace
  • getText(node)
  • setText(node,text)

This list is also the courses homework, and we’ll take a look at the results on Monday.

Tags: , , , , ,

Share on Twitter