document.body.appendChild(i)

2019-01-17 15:05发布

I am getting error only in IE7 as document.body is null, wehn i debug Microsoft script editor getting error in follwing line: ie

document.body.appendChild(i) code:

function nm_eraseCookie(name){
    nm_createCookie(name,"",-1)
}
var i=document.createElement('IMG');
i.src='//e.netmng.com/pixel/?aid=403';
i.width=1;
i.height=1;
document.body.appendChild(i);
nm_createCookie('nm_belgacom_bt',
escape('tv1=bun_intvtel;tv2=;tv3=;phone1=hbs_discoveryline;phone2=hbs_classical_line;phone3=;inet1=bun_nettvmob;inet2=hbs_adsl_res_plus;inet3=hbs_adsl_res_go;nm_banner=;nm_popin=hbs_discoveryline;'),183);

Can you infomr me what i need to do to solve this issue.

3条回答
何必那么认真
2楼-- · 2019-01-17 15:15

You could try

document.getElementsByTagName('body')[0].appendChild(i);

Now that won't do you any good if the code is running in the <head>, and running before the <body> has even been seen by the browser. If you don't want to mess with "onload" handlers, try moving your <script> block to the very end of the document instead of the <head>.

查看更多
唯我独甜
3楼-- · 2019-01-17 15:16

I know it is not with just plain js, but I had a similar issue and I used jquery. I just put all of my code inside a document ready() function

$( document ).ready(function() {
    //your code goes here
});

I hope it helps

查看更多
姐就是有狂的资本
4楼-- · 2019-01-17 15:28

It is working. Just modify to null check:

if(document.body != null){ document.body.appendChild(element); }

Pointy's suggestion is good; it may work, but I didn't try.

查看更多
登录 后发表回答