JavaScript code to generate a document map/table of contents from the document structure.
NB – Sorry I need to update this to get the examples working in WordPress
This page generates the following document map:
[jscript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
[brushjs] function get_my_navigation(use_body, html_text, do_bookmarks, heading_level, show_level) { //var all_elements = document.getElementsByTagName("*"); var all_elements; if (use_body == 1) all_elements = document.getElementsByTagName("*"); else if (html_text!=""){ html_div = document.createElement("div"); html_div.innerHTML = html_text; all_elements = html_div.getElementsByTagName("*"); } if ((heading_level==null) || (heading_level=="")) heading_level=3; if (show_level==null) show_level=0; var hopen="<h"+heading_level+">"; var hclose="</h"+heading_level+">"; structure = ""; first_level = 0; level_no = 0; this_level = 0; last_level = 0; current_level = 0; for (i = 0; i < all_elements.length; i++) { h_message = ""; element_text = ""; a = all_elements[i]; if (document.all) { element_text = a.innerText; } else { element_text = a.textContent; } node_name = all_elements[i].nodeName; if (node_name == "H1" || node_name == "H2" || node_name == "H3" || node_name == "H4" || node_name == "H5" || node_name == "H6") { level_no++; this_level = parseInt(all_elements[i].nodeName.substring(1, 2)); if (level_no == 1) first_level = this_level; if (show_level==1) element_text+=" ("+this_level+")"; // if we are bookmarking the contents if (do_bookmarks==1) element_text = "<a href='#generated_link_" + i + "'>" + element_text + "</a>"; if (this_level == last_level) { if (first_level == this_level) structure += hopen + element_text + hclose; else structure += "<li>" + element_text + "</li>"; } else if (this_level > last_level) { current_level++; difference = this_level - last_level; if (difference > 1) { h_message = " (bad heading structure)</li>"; } if (first_level == this_level) structure += hopen + element_text + hclose; else structure += "<ul><li>" + element_text + h_message + "</li>"; } else if (this_level < last_level) { difference = last_level - this_level; for (z = 0; z < difference; z++) { current_level--; structure += "</ul>"; } structure += "</ul>"; if (current_level < 0) structure += "<li>" + element_text + "</li> (bad heading structure)"; else { if (first_level == this_level) structure += hopen + element_text + hclose; else structure += "<ul><li>" + element_text + "</li>"; } } if (do_bookmarks == 1) { var lnk = document.createElement("a"); lnk.name = "generated_link_" + i; lnk.id = "generated_link_" + i; all_elements[i].appendChild(lnk); } last_level = this_level; } } for (z = 0; z < current_level; z++) { structure += "</ul>"; } return structure; } |
You’ll want a target div for the table of contents somewhere on your page:
1 2 3 |
[brushxml] <div id='table_of_contents_target'>Your TOC will go here</div> |
Then call the function at the end of a page or stick a window.onload function in:
1 2 3 4 5 6 7 |
[brushjs] window.onload=function() { document.getElementById("table_of_contents_target").innerHTML = get_my_navigation(1, '', 1, 3, 0); } |
Parameters
The parameters are pretty straight forward:
- use_body
- 1 will take everything in the body of the document
- 0 if you are going to pass HTML in
- html_text
- The HTML you want it to use if you aren’t using the whole document. If you aren’t passing any in, just leave it blank – “”. You can just pass in the html of a particular div like this: document.getElementById(“a_div”).innerHTML. I should probably change it so you can just pass a div ID in..
- do_bookmarks
- 1 if you want the document map hyper-linked to the headings. (This will add anchors at the heading elements).
- 0 will just give a plain text listing.
- heading_level
- The first item in the listing will be a heading, followed by nested lists. This sets the heading level you want it to be. (most likely 2 or 3). The default is 3.
- show_level
- If you set this to 1 it will add the heading level to the end of the link (like in the example on this page.
- 0 will leave it off
It would probably be better to replace use_body with an element ID and do it that way, but I haven’t got round to doing that yet..
Example..
Some headings just to show how the toc works.
A heading
Blah blah blah
A sub heading
Blah blah blah
You Sire! are a genious!