^^ This table of contents right here ^^ isn't actually in the document source. See for yourself.
For long, heavily-edited HTML documents, it becomes a major nuisance to keep a table of contents up to date. Perhaps even more annoying is keeping all of the section numbers contiguous and increasing. I offer a JavaScript solution here that dynamically builds a table of contents from the headings in a document and prepends legal-style section numbers to each of the headings.
The effect of the script is evident on this page. You can examine the HTML for this page using the View Source command in your web browser. You should also look at the JavaScript code.
The script adheres to the W3C JavaScript DOM specification, but of course that says very little in the world of non-compliant browsers. I have tested the script and verified that it works in Internet Explorer 6 on Windows and in Mozilla Firefox 1.5 on Linux. Support for other browsers is untested. The script should degrade adequately on non-supporting browsers: it would probably produce a JavaScript error, but the page itself would not be left unusable.
All you need to do to write a page that uses this script is to include the JavaScript code and add a call to generateTOC in your body's onload event handler:
<head>
<script src="toc.js" type="text/javascript"></script>
</head>
<body onload="generateTOC(document.getElementById('toc'));">
The document.getElementById('toc') tells the generateTOC function where you want it to insert the table of contents. The referenced element can be anything that can contain a <ul>.
<div id="toc"></div>
This page uses a stylesheet to suppress the list markers that would normally adorn the list items.
div#toc ul {
list-style-type: none;
}
Adding sections to your page could not be easier. Simply use a heading element. The text of your heading will automatically be captured and used by the script in generating the table of contents. Note that the script only processes heading levels 2 through 4 (<h2>, <h3>, <h4>); heading level 1 is reserved for the page title, and heading levels 5 and 6 are considered too insignificant to merit inclusion in the table of contents.
Important: You must not skip heading levels in the natural containment hierarchy, or the script will break. For example, you may not follow an <h2> with an <h4> without an intervening <h3>. However, it is permissible to follow an <h4> with an <h2> without an intervening <h3>.
The generateTOC function is actually quite simple once you get over all the DOM syntax. Pseudocode is presented here to demonstrate the algorithm:
<ul> element to the given element<h4>, then begin
<ul> element to the last item in the current level 3 list<li> element containing a link to the heading<h3>, then begin
<ul> element to the last item in the current level 2 list<li> element containing a link to the heading<h2>, then begin
<li> element containing a link to the heading