1 // from http://www.quirksmode.org/dom/toc.html
\r
3 function getElementsByTagNames(list,obj) {
\r
4 if (!obj) var obj = document;
\r
5 var tagNames = list.split(',');
\r
6 var resultArray = new Array();
\r
7 for (var i=0;i<tagNames.length;i++) {
\r
8 var tags = obj.getElementsByTagName(tagNames[i]);
\r
9 for (var j=0;j<tags.length;j++) {
\r
10 resultArray.push(tags[j]);
\r
13 var testNode = resultArray[0];
\r
14 if (!testNode) return [];
\r
15 if (testNode.sourceIndex) {
\r
16 resultArray.sort(function (a,b) {
\r
17 return a.sourceIndex - b.sourceIndex;
\r
20 else if (testNode.compareDocumentPosition) {
\r
21 resultArray.sort(function (a,b) {
\r
22 return 3 - (a.compareDocumentPosition(b) & 6);
\r
28 function createTOC() {
\r
29 // create the container to return
\r
30 var toc = document.createElement('div');
\r
31 toc.id = 'innertoc';
\r
33 var header = document.createElement('h2');
\r
34 header.innerHTML = 'Table of Contents';
\r
35 toc.appendChild(header);
\r
37 // set up the table of contents list
\r
38 var body = toc.appendChild(document.createElement('ol'));
\r
40 // grab the elements to add to the toc
\r
41 var toBeTOCced = getElementsByTagNames('h2,h3,h4,h5');
\r
44 if (toBeTOCced.length < 2) return false;
\r
46 // parent lists that subheaders get attached to
\r
47 var parents = new Array(false, body);
\r
49 // what level are we currently expecting?
\r
50 var current_level = 2;
\r
52 // process the lines
\r
53 for (var i = 0; i < toBeTOCced.length; i++) {
\r
56 var line = document.createElement('li');
\r
57 var link = document.createElement('a');
\r
58 line.appendChild(link);
\r
59 link.innerHTML = toBeTOCced[i].innerHTML;
\r
61 var level = toBeTOCced[i].nodeName.substr(1,1);
\r
63 while (level > current_level) {
\r
64 var sublist = document.createElement('ol');
\r
65 parents[current_level-1].appendChild(sublist);
\r
66 parents[current_level] = sublist;
\r
69 current_level = level;
\r
70 parents[current_level-1].appendChild(line);
\r
72 // setup the anchors
\r
73 var headerId = toBeTOCced[i].id || 'link' + i;
\r
74 link.href = '#' + headerId;
\r
75 toBeTOCced[i].id = headerId;
\r
80 var TOCstate = 'none';
\r
82 function showhideTOC() {
\r
83 TOCstate = (TOCstate == 'none') ? 'block' : 'none';
\r
84 var newText = (TOCstate == 'none') ? 'show page contents' : 'hide page contents';
\r
85 document.getElementById('contentheader').innerHTML = newText;
\r
86 document.getElementById('innertoc').lastChild.style.display = TOCstate;
\r
89 function loadTOC() {
\r
90 document.getElementById('toc').appendChild(createTOC());
\r
93 if (window.addEventListener) {
\r
94 window.addEventListener("load", loadTOC, false);
\r
95 } else if (window.attachEvent) {
\r
96 window.attachEvent("onload", loadTOC);
\r