mazurov
4/30/2013 - 12:38 PM

Traverse all nodes in DOM

Traverse all nodes in DOM

var nextS = function(node) {
  if (node == null) return null;
  if (node.nextSibling == null) return nextS(node.parentNode);
  return node.nextSibling;
}
var nextNode = function(node) {
  if (node.childNodes.length == 0) {
    return nextS(node);
  }
  return node.childNodes[0];
};

var node = document.body;
  do {
    node = nextNode(node);
  } while (node != null)