// 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("
| Table cell 1 |
| Table cell 2 |
\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("