BumbleBee
BumbleBee

Reputation: 10779

unable to set property 'innerHTML' of undefined or null reference. IE

I am trying to post a form and open in a new window. Works fine on Chrome and Firefox getting the following error in IE.

I am getting the following error in IE only. Can somebody please help me out finding a solution or alternate.

enter image description here

code :

function SubmitForm(actionUrl, Nums ) {
 var sHTML = "<form id='form1' action='" + actionUrl + "' method='post' target='MLSmap'>";
    sHTML += "<input name= num type='hidden' value='" + Nums + "' />";   
    sHTML += "</form>";

    var frmTosubmt = window.open(actionUrl, "MLSmap","width:500px; height:700px");
    frmTosubmt.document.body.innerHTML = sHTML ;
    frmTosubmt.form1.submit();
}

Upvotes: 2

Views: 7706

Answers (3)

TheRealCasadaro
TheRealCasadaro

Reputation: 11

Move the Script Block that calls your JavaScript File to the botton of your HTML page beneath the closing body tag (.

We are getting the error unable to set property 'innerHTML' of undefined or null reference because, we are trying to execute our JavaScript file before our webpage is loaded. This means we are trying to write to a page that technically doesn't exist.

Upvotes: 0

BumbleBee
BumbleBee

Reputation: 10779

Tried the suggestion by jfriend00 "open a blank window and then fill in the contents" and that worked.

function (url, params, target) {
     var form = ['<form method="POST" id= "frmpopup" action="', url, '" target="', target, '">'];
        for (var key in params)
            form.push('<input type="hidden" name="', key, '" value="', params[key], '"/>');
        form.push('</form>');
        jQuery(form.join('')).appendTo('body')[0];

        window.open('', target, "width:500px; height:700px; resi");
        $("#frmpopup").submit();
}

Upvotes: 0

jfriend00
jfriend00

Reputation: 707218

When you open a window from a URL, you probably have to provide a little bit of time for the browser to process that URL and actually get the document open before you can reference its contents. Different browsers will handle that timing differently and it may also depend upon whether the contents is cached or not (you should test with both cached and non-cached URLs).

You could, for example do this:

var frmTosubmt = window.open(actionUrl, "NewWindow","width:500px; height:700px");
frmTosubmt.onload = function() {
    frmTosubmt.document.body.innerHTML = sHTML;
    fmrTosubmt.frmMap.submit();
}

But, if you're replacing the entire body contents, I wonder why you're bothering to open a URL at all. Why not just open a blank window and then fill in the contents? Or, if all you're trying to do is submit a form, why not just use an Ajax post from your current window? Why bother opening a new window.

Also, your screenshot shows frmTosubmt.form1.submit(), but that isn't what's in the HTML. Your code shows something different. Not sure why those are different?

Upvotes: 4

Related Questions