最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - unable to set property 'innerHTML' of undefined or null reference. IE - Stack Overflow

programmeradmin3浏览0评论

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.

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();
}

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.

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();
}
Share Improve this question edited Aug 14, 2014 at 22:43 BumbleBee asked Aug 14, 2014 at 22:35 BumbleBeeBumbleBee 10.9k20 gold badges80 silver badges124 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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?

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();
}

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.

发布评论

评论列表(0)

  1. 暂无评论