Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-13-2011, 08:27 PM   #1
Beta Member
 
Join Date: May 2011
Posts: 1
Unhappy whats wrong with this code...please help newbie


*/

function addEvent(object, evName, fnName, cap) {
if (object.attachEvent)
object.attachEvent("on" + evName, fnName);
else if (object.addEventListener)
object.addEventListener(evName, fnName, cap);
}

addEvent(window, "load", makeTOC, false);

var sections = new Array("h1", "h2", "h3", "h4", "h5", "h6");
var sourceDoc; // document on which the TOC is based

function makeTOC() {
var TOC = document.getElementById("toc");
TOC.innerHTML = "<h1>Table of Contents</h1>";
var TOCList = document.createElement("ol");
TOC.appendChild(TOCList);

sourceDoc = document.getElementById("doc");

// generate list items containing section headings
createList(sourceDoc, TOCList);
}

function levelNum(node) {
for (var i = 0; i < sections.length; i++) {
if (node.nodeName == sections[i].toUpperCase()) return i;
}
return -
1; // node is not a section heading
}

function createList(object, list) {

var prevLevel =
0; // level of the previous TOC entry
var headNum =
0; // running count of section headings

for (var n = object.firstChild; n != null; n = n.nextSibling) {
// loop through all of the nodes within object

var nodeLevel = levelNum(n);
if (nodeLevel != -
1) {
// node represents a section heading

//insert id for the section heading if necessary
headNum++;
if (n.id ==
"") {n.id = "head" + headNum;}

// create a list item to match
var listItem = document.createElement(
"li");

// Create a hypertext link to the section heading
var linkItem = document.createElement(
"a");
linkItem.innerHTML = n.innerHTML;
linkItem.href =
"#" + n.id;

// Append the hypertext link to the list entry
listItem.appendChild(linkItem);

if (nodeLevel == prevLevel) {
// append the entry to the current list
list.appendChild(listItem);
}

else if (nodeLevel > prevLevel) {
// append the entry to a new nested list
var nestedList = document.createElement("ol");
nestedList.appendChild(listItem);

list.lastChild.appendChild(nestedList);

// Add plus/minus box before the text of the nested list
var plusMinusBox = document.createElement("span");
plusMinusBox.innerHTML = "--";
addEvent(plusMinusBox, "click", expandCollapse, false);
nestedList.parentNode.insertBefore(plusMinusBox, nestedList.previousSibling);
list = nestedList;
prevLevel = nodeLevel;
}

else if (nodeLevel < prevLevel) {
// append the entry to a higher-level list
var levelUp = prevLevel - nodeLevel;
for (var i =
1; i <= levelUp; i++) {list = list.parentNode.parentNode;}

list.appendChild(listItem);
prevLevel = nodeLevel;

}
}

}
}

function expandCollapse(e) {
var plusMinusBox = e.target || event.srcElement;
var nestedList = plusMinusBox.nextSibling.nextSibling;

// Toggle the plus and minus symbol
if (plusMinusBox.innerHTML ==
"--") plusMinusBox.innerHTML = "+"
else plusMinusBox.innerHTML =
"--";

// Toggle the display style of the nested list
if (nestedList.style.display ==
"none") nestedList.style.display = ""
else nestedList.style.display =
"none";

// expand and collapse the source document to match the TOC
expandCollapseDoc();
}

function expandCollapseDoc() {
var displayStatus =
"";
for (var n = sourceDoc.firstChild; n != null; n = n.nextSibling) {
var nodeLevel = levelNum(n);

if (nodeLevel != -
1) {
// determine the display status of the TOC entry
var TOCentry = document.getElementById(
"TOC" + n.id);
if (isHidden(TOCentry)) displayStatus =
"none"
else displayStatus =
"";
}

if (n.nodeType ==
1) { // node represents a page element
// apply the current display status to the node
n.style.display = displayStatus;
}
}
}

function isHidden(object) {
for (var n = object; n.nodeName !=
"BODY"; n = n.parentNode) {
if (n.style.display ==
"none") return true;
}

return false;
}

newwebprogramer is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 03:32 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0