document.body.appendchild has a null value.

I am loading a .js file I wrote. Within that .js file I am creating a Iframe like this.

var frmSource = "http://MYLINK.com/mypage.html?" + encodeURI(URLBuilder);
    ifrm = document.createElement("IFRAME");
    ifrm.setAttribute("src", frmSource); a
    ifrm.style.width = 0 + "px";
    ifrm.style.height = 0 + "px";
    ifrm.setAttribute("frameBorder", "0");
    document.body.appendChild(ifrm);

URLBuilder contains the GET variables to the next page

The issue occurs at document.body.appendChild(ifrm);

and my error is javascript typeerror: null is not an object (evaluating 'document.body.appendchild')

I suspect the issue is it is trying to append the iframe to the body but the body has not properly loaded. I am currently only getting this issue in safari.

Last updated:3/23/2015 7:59:01 AM

1 Answers

Anonymous User
Anonymous User

I have done something like this to configure my IFrame creation and it is working fine.

var urlWithParam = url + encodeURI(URLBuilder)
    var iframe = document.createElement('iframe');
    iframe.src = urlWithParam;
    iframe.id = 'iframe_' + done;
    iframe.style.position = 'relative';
    iframe.style.height = '100%';
    iframe.style.width = '100%';
    iframe.style.top = '0';
    iframe.style.left = '0';
    iframe.style.right = '0';
    iframe.style.bottom = '0';
    iframe.style.frameBorder = '0';
    iframe.style.borderStyle = 'none';
    iframe.style.display = 'none;';
 
 
    //if you want to do something after the Iframe load
    iframe.onload = function (event) {
 
    };

Answer