/** toc.js This is a simplified version of the scipt "generated_toc.js" written by: Stuart Langridge, July 2007 The script is licensed under the terms of the MIT license. See the following page for details: http://www.kryogenix.org/code/browser/generated-toc/ Generate a table of contents, based on headings in the page. To place the TOC on the page, add
to the page where you want the TOC to appear. If this element is not present, the TOC will not appear. */ generated_toc = { generate: function() { // Identify our TOC element, and what it applies to generate_from = '2'; tocparent = document.getElementById('generated-toc'); if (!tocparent) { // They didn't specify a TOC element; exit return; } // set top_node to be the element in the document under which // we'll be analysing headings top_node = document.getElementsByTagName('body')[0]; // If there isn't a specified header level to generate from, work // out what the first header level inside top_node is // and make that the specified header level if (generate_from == 0) { first_header_found = generated_toc.findFirstHeader(top_node); if (!first_header_found) { // there were no headers at all inside top_node! return; } else { generate_from = first_header_found.toLowerCase().substr(1); } } // add all levels of heading we're paying attention to to the // headings_to_treat dictionary, ready to be filled in later headings_to_treat = {"h6":''}; for (var i=5; i>= parseInt(generate_from); i--) { headings_to_treat["h" + i] = ''; } // get headings. We can't say // getElementsByTagName("h1" or "h2" or "h3"), etc, so get all // elements and filter them ourselves // need to use .all here because IE doesn't support gEBTN('*') nodes = top_node.all ? top_node.all : top_node.getElementsByTagName('*'); // put all the headings we care about in headings headings = []; for (var i=0; i cur_head_lvl) { // this heading is at a lower level than the last one; // create additional nested lists to put it at the right level // get the *last* LI in the current list, and add our new UL to it var last_listitem_el = null; for (var j=0; j]+>/g, ''); }, findFirstHeader: function(node) { // a recursive function which returns the first header it finds inside // node, or null if there are no functions inside node. var nn = node.nodeName.toLowerCase(); if (nn.match(/^h[1-6]$/)) { // this node is itself a header; return our name return nn; } else { for (var i=0; i