homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

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

Msg#: 4559353 posted 9:31 pm on Mar 28, 2013 (gmt 0)

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?



WebmasterWorld Senior Member 5+ Year Member

Msg#: 4559353 posted 3:02 am on Mar 29, 2013 (gmt 0)

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 ) ...


Msg#: 4559353 posted 3:22 am on Mar 29, 2013 (gmt 0)

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]


WebmasterWorld Senior Member 5+ Year Member

Msg#: 4559353 posted 5:28 am on Mar 29, 2013 (gmt 0)

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>" ?


Msg#: 4559353 posted 5:57 am on Mar 29, 2013 (gmt 0)

getElementsByTagName('output')[0] entirely?
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?


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

Msg#: 4559353 posted 2:14 pm on Mar 29, 2013 (gmt 0)

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;


Msg#: 4559353 posted 2:49 am on Mar 31, 2013 (gmt 0)

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!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved