Code: Select all
This topic contains:
- something
- something else
- yet another thing
In case this is relevant, my topics all start with an H1 or H2 title. This is followed by a number of small sections, each of which has an H3 heading.
Matt
Code: Select all
This topic contains:
- something
- something else
- yet another thing
Ah, that makes sense. Yes, it would be nice to be able to create a mini TOC that would apply only to a topic.MattG wrote:I've tried that but with no luck: what you suggested creates a mini-TOC that lists all topics that are below the mini-TOC in the TOC tree. At least, that's what happened when I tried it.
I'm looking for a way to list all the H3 headings in the same topic as a mini-TOC.
Code: Select all
// Original JavaScript code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function buildMenu(target_id, heading_tag)
{
// abort if using MSIE (Mac) or DHTML functions not present
if(navigator.userAgent.indexOf("Mac_PowerPC") != -1) return;
if(!document.createElement) return;
// the element identified by target_id will contain the menu
var target = document.getElementById(target_id);
// scan the DOM for tags matching heading_tag (eg. 'H2')
var headings = document.getElementsByTagName(heading_tag);
if(headings.length > 1) {
// found two or more headings - start building ordered list
var menuList = document.createElement('UL');
// for each heading on the page
for(var i=0; i < headings.length; i++) {
// use exising id or add new id to heading to use as anchor
var anchorName = "section_" + i;
if(headings[i].id == '') {
headings[i].setAttribute('id', anchorName);
} else {
anchorName = headings[i].id;
}
// extract text from heading
var headingText = headings[i].firstChild.nodeValue
// prefix heading with number
// headings[i].firstChild.nodeValue = (i+1) + ". " + headingText;
// create link to heading
var menuLink = document.createElement('A');
menuLink.setAttribute('href', '#' + anchorName);
menuLink.appendChild(document.createTextNode(headingText));
// create list item to contain link
var listItem = document.createElement('LI');
listItem.appendChild(menuLink);
// append list item to list
menuList.appendChild(listItem);
}
// remove contents of target element
while(target.hasChildNodes()) target.removeChild(target.firstChild);
// insert list into target element
target.appendChild(menuList);
} else {
// no headings found - remove target element from page
target.parentNode.removeChild(target);
}
}
Code: Select all
<script type="text/javascript" src="buildmenu.js"></script>
Code: Select all
<body onLoad="buildMenu('target_id', 'h2');">
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="3" MadCap:lastHeight="80" MadCap:lastWidth="752">
<body>
<script type="text/javascript" src="buildMenu.js" />
<script type="text/javascript">
function buildmenu_init() { buildMenu('submenu', 'h3'); }
var oldonload = window.onload;
if(typeof window.onload != 'function') {
window.onload = buildmenu_init;
} else {
window.onload = function() {
oldonload(); buildmenu_init();
}
}
</script>
<table class="inThisTopic">
<tbody>
<tr>
<td>
<p>In this topic...</p>
<div id="submenu">building menu...</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Code: Select all
// Original JavaScript code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
function buildMenu(target_id, heading_tag)
{
// abort if using MSIE (Mac) or DHTML functions not present
if(navigator.userAgent.indexOf("Mac_PowerPC") != -1) return;
if(!document.createElement) return;
// the element identified by target_id will contain the menu
var target = document.getElementById(target_id);
// scan the DOM for tags matching heading_tag (eg. 'H2')
var headings = document.getElementsByTagName(heading_tag);
if(headings.length > 1) {
// found two or more headings - start building ordered list
var menuList = document.createElement('UL');
// for each heading on the page
for(var i=0; i < headings.length; i++) {
// use exising id or add new id to heading to use as anchor
var anchorName = "section_" + i;
if(headings[i].id == '') {
headings[i].setAttribute('id', anchorName);
} else {
anchorName = headings[i].id;
}
// Extract text from heading.
// MODIFICATION
// In the original Chirp Internet code, this used firstChild instead
// of lastChild. However, Flare sometime starts headings with a
// bookmark, so using lastChild means that the ignores the bookmark
// at the start of the heading.
var headingText = headings[i].lastChild.nodeValue
// prefix heading with number
// headings[i].lastChild.nodeValue = (i+1) + ". " + headingText;
// create link to heading
var menuLink = document.createElement('A');
menuLink.setAttribute('href', '#' + anchorName);
menuLink.appendChild(document.createTextNode(headingText));
// create list item to contain link, set it's class
// and append it to the menu.
var listItem = document.createElement('LI');
listItem.setAttribute("class", "table1");
listItem.appendChild(menuLink);
// append list item to list
menuList.appendChild(listItem);
}
// remove contents of target element
while(target.hasChildNodes()) target.removeChild(target.firstChild);
// insert list into target element
target.appendChild(menuList);
} else {
// no headings found - remove target element from page
target.parentNode.removeChild(target);
}
}
Code: Select all
<h3><a name="ThisIsABookmark"></a>Some Text</h3>
Code: Select all
var headingText = headings[i].firstChild.nodeValue
Code: Select all
var headingText = headings[i].lastChild.nodeValue
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="383" MadCap:lastWidth="807">
<head>
<script type="text/javascript" src="buildmenu.js">
</script>
</head>
<body>
<body onLoad="buildMenu('target_id', 'h2');">
<h1>testing topic</h1>
<div id="submenu" style="border: 2px solid blue;">building menu...</div>
<script type="text/javascript" src="/buildmenu.js">
</script>
<script type="text/javascript">
function buildmenu_init() { buildMenu('submenu', 'h2'); }
var oldonload = window.onload;
if(typeof window.onload != 'function') {
window.onload = buildmenu_init;
} else {
window.onload = function() {
oldonload();
buildmenu_init();
}
}
</script>
<h2>Delete this text and replace it with your own content.</h2>
<h2>griddle</h2>
<h2>Middle</h2>
<h2>fiddle</h2>
<h2>piddle</h2>
<p> </p>
</body>
</body>
</html>
Probably not, since the Preview mode isn't a full browser. It used to be you couldn't even do some of the Flare effects in the Preview window or click on a link to test it, although most of that is supported now, I believe. Try building the output and see if it works.jcoria wrote:Or can you not see the appropriate output in Preview mode?
Code: Select all
<script type="text/javascript" src="/buildmenu.js">