// F. Permadi 2005. // Print DOM tree // (C) F. Permadi function runTest() { var outputWindow=window.open(); var myDocument=outputWindow.document.open("text/html", "replace"); myDocument.writeln("Html Title"); myDocument.writeln("
Table cell 1
Table cell 2
"); myDocument.writeln(""); myDocument.close(); printDOMTree(myDocument.getElementById("testhtml")); outputWindow.close(); } //////////////////////////////////////////// // This function accepts a DOM element as parameter and prints // out the descendant DOM structure of the element. //////////////////////////////////////////// function printDOMTree(domElement) { var outputWindow=window.open(); outputWindow.document.open("text/html", "replace"); var myElement=domElement; outputWindow.document.write("Dom Tree\n"); outputWindow.document.write("\n"); traverseDOMTree(outputWindow.document, myElement, 1); outputWindow.document.write("\n"); outputWindow.document.write("\n"); // Must close document, otherwise Mozilla browsers keeps showing loading progress outputWindow.document.close(); } //////////////////////////////////////////// // This function prints the DOM tree of an element. // This function is to be called recursively until the DOM tree is fully traversed. // // Parameters: // targetDocument is where the tree will be printed into // currentElement is the element that we want to print // depth is the depth of the current element (it should be 1 for the initial element) //////////////////////////////////////////// function traverseDOMTree(targetDocument, currentElement, depth) { if (currentElement) { var j; var tagName=currentElement.tagName; if (tagName) targetDocument.writeln("<"+currentElement.tagName+">"); else targetDocument.writeln("[unknown tag]"); var i=0; var currentElementChild=currentElement.childNodes[i]; while (currentElementChild) { targetDocument.write("
\n"); for (j=0; j"); for (j=0; j"); for (j=0; j