Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

TypeError: document.getElementsByTagName(.)[0] is undefined



9:31 pm on Mar 28, 2013 (gmt 0)

5+ Year Member

I'm working on a friends' site, and I keep getthing this in Firefox's Error console:
Error: TypeError: document.getElementsByTagName(...)[0] is undefined

How can I define it? I didn't write this (I know zip about javascript)

I've tried:
var getElementsByTagName;
and a mix of other concoctions; nothing seems to work.

I'm assuming that simply assigning a blank variable (or the equivalent of php's isset()) would get rid of this error.

Any way someone can help guide me in the right direction?


3:02 am on Mar 29, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

You have a statement like ... document.getElementsByTagName('foo')[0] ... somewhere in the javascript.

It is failing trying to find the first foo tag in you html.

So either
- add foo statement to html
- remove offending javascript
- make javascript conditional

var fooTags = document.getElementsByTagName('foo')
if ( fooTags.length > 0 ) ...


3:22 am on Mar 29, 2013 (gmt 0)

5+ Year Member

I tried this:
var output = document.getElementsByTagName('output');

No luck.

This is the line that has the getElements:
document.getElementsByTagName('output')[0].innerHTML = 'screen.width:'+screen.width+'<br>screen.height:'+screen.height+'<br>window.innerWidth:'+window.innerWidth+'<br>window.innerHeight:'+window.innerHeight+'<br>
window.outerWidth:'+window.outerWidth+'<br>window.outerHeight:'+window.outerHeight+'<br>document.documentElement.<br> clientWidth:'+document.documentElement.clientWidth+'<br>
document.documentElement.<br> clientHeight:'+document.documentElement.clientHeight+'<br>window.devicePixelRatio:'+window.devicePixelRatio; }

[edited by: engine at 10:00 am (utc) on Mar 29, 2013]
[edit reason] sidecroll [/edit]


5:28 am on Mar 29, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

It expects to find in html


and replaces foo with "screen ..."

This looks like debug, take the getElements line out ?
else add "<output>foo</output>" ?


5:57 am on Mar 29, 2013 (gmt 0)

5+ Year Member

So that part would look like:
document.[0].innerHTML = 'screen.width:'+screen.width+'<br>screen.height:'+screen.height+'....


else add "<output>foo</output>" ?

I apologize for the dumb question, but how do I go about doing that?


2:14 pm on Mar 29, 2013 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

brokaddr, daveVk was saying that the entire line looks like it's something that's being used for debugging purposes, and therefore the entire line might safely be removed. Otherwise, you need to have an <output> element somewhere on the page in your markup. Note, IE does not support the <output> element.

Here's another alternative. Replace that line with this entire section:

var outputElements = document.getElementsByTagName('output');
if (outputElements.length > 0) {
outputElements[0].innerHTML = 'screen.width:' + screen.width +
'<br>screen.height:' + screen.height +
'<br>window.innerWidth:' + window.innerWidth +
'<br>window.innerHeight:' + window.innerHeight +
'<br>window.outerWidth:' + window.outerWidth +
'<br>window.outerHeight:' + window.outerHeight +
'<br>document.documentElement.<br> clientWidth:' +
document.documentElement.clientWidth +
'<br>document.documentElement.<br> clientHeight:' +
document.documentElement.clientHeight +
'<br>window.devicePixelRatio:' + window.devicePixelRatio;


2:49 am on Mar 31, 2013 (gmt 0)

5+ Year Member

Ohh ok. Thanks for the clarification!
I doubt my friend is using a debugging script, more likely that this is residual code from a template he was using eons ago.

I'll comment it out and see if he complains, if not; I will purge the code.

If I run into a similar issue in the future, I will at least know how to define a js variable :)

Thanks guys!

Featured Threads

Hot Threads This Week

Hot Threads This Month