javascript - The source of the error : document.getElementsByTagName(...)[0] is undefined - Stack Overflow

I'm using an awstats js tracker file awstats_misc_tracker.js and as soon as I include it, it shows

I'm using an awstats js tracker file awstats_misc_tracker.js and as soon as I include it, it shows this error in my console :

TypeError: document.getElementsByTagName(...)[0] is undefined
     document.getElementsByTagName("body")[0].appendChild(l);

Here's the part that render this error :

var imgsrc1 = awstatsmisctrackerurl+'?screen='+TRKscreen+'&win='+TRKwinsize+'&cdi='+TRKcdi+'&java='+TRKjava;
var imgsrc2 = '&shk='+TRKshk+'&svg='+TRKsvg+'&fla='+TRKfla+'&rp='+TRKrp+'&mov='+TRKmov+'&wma='+TRKwma+'&pdf='+TRKpdf+'&uid='+TRKuserid+'&sid='+TRKsessionid;
//alert(imgsrc1);
//alert(imgsrc2);
var imgsrc=imgsrc1+imgsrc2;
if( document.createElementNS ) {
    var l=document.createElementNS("","img");
    l.setAttribute("src", imgsrc );
    l.setAttribute("height", "0");
    l.setAttribute("width", "0");
    l.setAttribute("border", "0");
    document.getElementsByTagName("body")[0].appendChild(l);
} else {
    document.write('<img style="display:none;" src="'+ imgsrc +'" height="0" width="0" border="0" />')
}

Can anyone please show me the sourse of this error and how I can prevent it ?

I'm using an awstats js tracker file awstats_misc_tracker.js and as soon as I include it, it shows this error in my console :

TypeError: document.getElementsByTagName(...)[0] is undefined
     document.getElementsByTagName("body")[0].appendChild(l);

Here's the part that render this error :

var imgsrc1 = awstatsmisctrackerurl+'?screen='+TRKscreen+'&win='+TRKwinsize+'&cdi='+TRKcdi+'&java='+TRKjava;
var imgsrc2 = '&shk='+TRKshk+'&svg='+TRKsvg+'&fla='+TRKfla+'&rp='+TRKrp+'&mov='+TRKmov+'&wma='+TRKwma+'&pdf='+TRKpdf+'&uid='+TRKuserid+'&sid='+TRKsessionid;
//alert(imgsrc1);
//alert(imgsrc2);
var imgsrc=imgsrc1+imgsrc2;
if( document.createElementNS ) {
    var l=document.createElementNS("http://www.w3/1999/xhtml","img");
    l.setAttribute("src", imgsrc );
    l.setAttribute("height", "0");
    l.setAttribute("width", "0");
    l.setAttribute("border", "0");
    document.getElementsByTagName("body")[0].appendChild(l);
} else {
    document.write('<img style="display:none;" src="'+ imgsrc +'" height="0" width="0" border="0" />')
}

Can anyone please show me the sourse of this error and how I can prevent it ?

Share Improve this question asked May 19, 2014 at 13:34 user3350731user3350731 9721 gold badge11 silver badges30 bronze badges 4
  • 2 Do you have a <body> tag at the time the JS executes? Perhaps you're including the script file in the header and not in the bottom of your body? – h2ooooooo Commented May 19, 2014 at 13:35
  • has the dom been loaded and is ready? – Daniel A. White Commented May 19, 2014 at 13:36
  • 1 To give you a pretty literal answer -- the source of the error is that document.getElementsByTagName("body")[0] is undefined, which means there isn't a body element at the time it's executing. Now you just have to figure out why. – Sam Hanley Commented May 19, 2014 at 13:37
  • you don't really need createElementNS. document.createElement('img') or just var img = new Image(); should do it. – gp. Commented May 19, 2014 at 13:38
Add a ment  | 

1 Answer 1

Reset to default 5

You are including the script on the head prior to definition of the body.

The script is parsed and executed as it is found in the HTML file.

You should include this script after the body has been declared.

A remended location to include all the javascript is at bottom of the HTML file, but it sometimes break legacy code.

Another way is to execute the script after on the onload page event or use query and the 'ready' function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745588810a4634708.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信