...

View Full Version : getElementsByTagName is not a function?



DanoMano
06-30-2006, 06:35 PM
Javascript/html noob here..any help greatly appreciated.

Here's the deal, If I access the page directly on my pc via the file system (i.e. I open up the file via: file::/path../main.html, the page and script runs just fine.

If however I point the browser to tomcat i.e. localhost:8080/blah
firefox spits out the error: getElementsByTagName is not a function.

Any thoughts or pointers? I'm totally confused over this..this is a built in function call?

note: I did an instanceof on the object making the call, and it does indeed confirm it is an object.

ps. I have confirmed I can access the all scripts/css files from the webserver (tomcat)

System:
FireFox (1.5.04), Tomcat, IE6 (also fails, even though I haven't found out how to see the js error details?)

jskaar
06-30-2006, 06:41 PM
A little code with the implemention would be useful to see what may be wrong. Have you for instance remembered to use document in front of the call? Like document.getElementsByTagName()

ealbrecht
06-30-2006, 06:45 PM
It would help more to show a chunk of the sample code that where it is failing.

The getElementsByTagName() method will only work on either the document object (document) or on html element nodes (p, a, div, etc...)


/* returning a collection of elements from the document object */
var pArray = document.getElementsByTagName('p');

/* using an element to return a collection of elements
<p id="myP"><img ... /></p>
*/
var imgArray = document.getElementById('myP').getElementsByTagName('img');

DanoMano
06-30-2006, 06:54 PM
The code in question was:

for (var k = 0; k < objs.length; k++)
{
var table = objs[k];
alert(typeof table.getElementsByTagName("tbody"));
var tbodies = table.getElementsByTagName("tbody");
}

On a hunch (I just recalled I hadn't fixed something prior on that page, pulled up another page using the same script..it worked)

it turns out table while it was an 'object' I guess, was not defined, I added an:

if (!table) check, and the js error went away, while I now know how to fix the problem, I'm still curious: why would:

log.debug("table="+table); In the above code result in table=1 (log4j.js)
I would have expected that it would have shown either undefined, or null??

Thanx again all the quick responses.

error792
06-30-2006, 08:07 PM
Try replacing this line:


alert(typeof table.getElementsByTagName("tbody"));

witht this:


alert(table);

as Firefox will tell you something more informative than 'object', probably '[object HTMLTableElement]'; also I think that you are trying to ensure that the table variable is a Node? The code you posted would be checking the type of the result of the getElementsByTagName function call - a NodeList.

Is the objs variable a list of elements? If so, try objs.item(k) instead of objs[k].

ealbrecht
07-04-2006, 04:50 PM
The source of the problem is the objs Array. In your code you are literally "expecting" there to be table elements in each objs array index. How did you derive the objs Array? Are you certain that each objs array index contains data. Putting a conditional check on the table variable suggests that not all indexes of the objs array contains data because only things like null, 0, false, and undefined should return false inside of the IF statement.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum