PDA

View Full Version : Newbie needs javascript help



spijam23
Jun 6th, 2010, 06:49 PM
Hello, everyone i have a mini problem on my hands. I wrote a javascript file, which was linked to a html file. The issue here is that i cannot seem to load my table of contents. i have checked my syntax countless times and my compiler shows no error message. i hope anyone of you out there could help me out. thanks!

here are the codes:

unsconttxt.htm -


<?xml version="1.0" encoding="UTF-8" ?>

toctxt.js -


/*

Author:
Date:

Filename: toctxt.js

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); //the code to run makeTOC() function when the page loads

var sections = new Array("h1", "h2", "h3", "h4", "h5", "h6"); //list of elements that act as section headings
var sourceDoc; //document on which the TOC is based

function makeTOC()
{
var TOC = document.getElementById("toc"); //html codes added to the TOC object.
TOC.innerHTML = "<h1>Table of Contents</h1>";
var TOCList = document.createElement("ol"); //creates an element node for the ol element
TOC.appendChild(TOCList); //appends the ol element to the table of contents element

sourceDOC = document.getElementById("doc");

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

function levelNum(node) //this function returns the level number of the section heading or -1 if not a section heading.
{
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) //this function creates the list items for the table of contents
{
for (var n = object.firstCHild; n != null; n = n.nextsibling)
{
//loop through all of the nodes within the object

var nodeLevel = levelNum(n); //locates section headings within an object
if (nodeLevel != -1) //locates section headings within an object
{
//node represents a section heading
//creates a list item to match
var listItem = document.createElement("li"); //text of the list item comes from the text of the section heading
listItem.innerHTML = n.innerHTML; //text of the list item comes from the text of the section heading
list.appendChild(listItem); //text of the list item comes from the text of the section heading
}
}
}

jmrker
Jun 6th, 2010, 07:47 PM
Looks like you never close the comment section


/*

Author:
Date:

Filename: toctxt.js

*/

function addEvent(object, evName, fnName, cap) {
...

spijam23
Jun 6th, 2010, 07:59 PM
Hey, thanks for the reply. I did close the comments. My mistake though, because i was copying the codes from the microsoft visual studio program. Are there any other reasons?

jmrker
Jun 6th, 2010, 08:14 PM
Do you have a link to the whole code?

spijam23
Jun 6th, 2010, 08:19 PM
No i don't but i could email you if that's alright?

jmrker
Jun 6th, 2010, 09:57 PM
Just post your code or attach as a .txt file.
I might not be able to help and others might have an interest in your problem.

JS is not a private language.
If what you are trying to do is supposed to be secure, then JS is not the language for your task.

spijam23
Jun 7th, 2010, 05:47 AM
okay, my mistake. I did not know i could attach it. I have attached the two files. I deleted some content from the usconsttxt file as it was too big. Thanks.

jmrker
Jun 7th, 2010, 03:03 PM
I'm gettng an error in the toctxt.js file at


function createList(object, list) //this function creates the list items for the table of contents
{
for (var n = object.firstCHild; n != null; n = n.nextsibling)

on the "for" line indicating


Error: object is undefined
Source File: file:///Volumes/STORE%20N%20GO/Constitution/toctxt.js
Line: 84

What exactly are you trying do do here?
:confused:

spijam23
Jun 7th, 2010, 03:36 PM
I am trying to populate the table of contents container with the headings <h1> to <h3>. the end product should have the 'table of contents' as the first line, as shown in usconsttxt.htm. The next few lines are followed by 'Articles of the constitution', 'Article 1 - The legislative branch', etc.