JavaScript DHTML/Page Components/Table Of Content
Create a table of contents(TOC) for this document
<source lang="html4strict">
/* Examples From JavaScript: The Definitive Guide, Fourth Edition Legal matters: these files were created by David Flanagan, and are Copyright (c) 2001 by David Flanagan. You may use, study, modify, and distribute them for any purpose. Please note that these examples are provided "as-is" and come with no warranty of any kind. David Flanagan
- /
/**
* Create a table of contents for this document, and insert the TOC into * the document by replacing the node specified by the replace argument. **/
function maketoc(replace) {
// Create a element that is the root of the TOC tree
element that will hold the TOC and add it
var table = document.createElement("table");
toc.appendChild(table);
// Create a <tbody> element that holds the rows of the TOC
var tbody = document.createElement("tbody");
table.appendChild(tbody);
// Initialize an array that keeps track of section numbers
var sectionNumbers = [0,0,0,0,0,0];
// Recursively traverse the body of the document, looking for sections
// marked with
var toc = document.createElement("div"); // Set a background color and font for the TOC. We"ll learn about // the style property in the next chapter toc.style.backgroundColor = "white"; toc.style.fontFamily = "sans-serif"; // Start the TOC with an anchor so we can link back to it. var anchor = document.createElement("a"); // Create an <a> node anchor.setAttribute("name", "TOC"); // Give it a name toc.appendChild(anchor); // And insert it // Make the body of the anchor the title of the TOC anchor.appendChild(document.createTextNode("Table Of Contents"));// Create a