var loc = new Array(); var myAs = new Array(); var aSett = new Array(); var text = new Array(); var first=0; var first2=0; var otherfirst=0; var winn; var win; var anchor2 = new Array(); var num = 0; var sectionNumber = new Array(); var maindoc; var impElement = new Array(); var aSet; var sectionTitle = new Array(); var win2; var d; var toc; var container; var second = 0; function maketoc() { // Find the container. If there isn't one, return silently. container = toc; if (first != 0) return; // Traverse the document, adding all

...

tags to an array var sections = []; findSections(maindoc, sections); // Initialize an array that keeps track of section numbers var sectionNumbers = [0,0,0,0,0,0]; // Now loop through the section header elements we found for(var s = 0; s < sections.length; s++) { var section = sections[s]; // Figure out what level heading it is var level = parseInt(section.tagName.charAt(1)); if (isNaN(level) || level < 1 || level > 6) continue; // Increment the section number for this heading level // And reset all lower heading level numbers to zero sectionNumbers[level-1]++; for(var i = level; i < 6; i++) sectionNumbers[i] = 0; // Now combine section numbers for all heading levels // to produce a section number like 2.3.1 var sectionNumber = ""; for(i = 0; i < level; i++) { sectionNumber += sectionNumbers[i]; if (i < level-1) sectionNumber += "."; } // Add the section number and a space to the section header title. // We place the number in a to make it styleable. var frag = maindoc.createDocumentFragment(); // to hold span and space var span = maindoc.createElement("span"); // span to hold number span.className = "TOCSectNum"; // make it styleable span.appendChild(maindoc.createTextNode(sectionNumber)); // add sect# frag.appendChild(span); // Add span to fragment frag.appendChild(maindoc.createTextNode(" ")); // Then add a space if(second == 0)section.insertBefore(frag, section.firstChild); // Add both to header // Create an anchor to mark the beginning of this section. var anchor = maindoc.createElement("a"); anchor.name = "TOC"+sectionNumber; // Name the anchor so we can link anchor.id = "TOC"+sectionNumber; // In IE generated anchors need ids // Insert the anchor and link immediately before the section header section.parentNode.insertBefore(anchor, section); // Now create a link to this section. var link = d.createElement("a"); link.name = "TOC"+sectionNumber; link.href = ""; // Set link destination link.innerHTML = section.innerHTML; // Make link text same as heading // Place the link in a div that is styleable based on the level var entry = d.createElement("div"); entry.className = "TOCEntry TOCLevel" + level; // For CSS styling entry.appendChild(link); // And add the div to the TOC container container.appendChild(entry); } // This method recursively traverses the tree rooted at node n, looking // for

through

tags and appends them to the sections array. function findSections(n, sects) { // Loop through all the children of n for(var m = n.firstChild; m != null; m = m.nextSibling) { // Skip any nodes that are not elements. if (m.nodeType != 1 /* Node.Element_NODE */) continue; // Skip the container element since it may have its own heading // if (m == container) continue; // As an optimization, skip

tags since headings are not // supposed to appear inside paragraphs. (We could also skip // lists,

 tags, etc., but 

is the most common one.) if (m.tagName == "P") continue; // optimization // If we didn't skip the child node, check whether it is a heading. // If so, add it to the array. Otherwise, recurse on it. // Note that the DOM is interface-based not class-based so we // cannot simply test whether (m instanceof HTMLHeadingElement). if (m.tagName.length==2 && m.tagName.charAt(0)=="H") sects.push(m); else findSections(m, sects); } } second = 1; aSet = d.getElementsByTagName("a") for(w=0;wTable of Contents'); d = win.document; toc = d.createElement("div"); toc.setAttribute("id", "toc"); d.body.appendChild(toc); } maketoc(); win.opener.first = 2; d.close(); } function closeThem() { if(win) win.close(); }