javascriptinternet-explorer-7

How to handle document.body being null on IE7 when trying to call appendChild on it


I am getting error specific to Internet Explorer 7 due to document.body being null on that platform.

The error happens when I try to do document.body.appendChild(i) in the following 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);

How can I solve this issue?


Solution

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