简体   繁体   English

无法设置属性“ innerHTML”的值:计时问题?

[英]Unable to set value of the property 'innerHTML': timing issue?

Others have encountered a similar error, usually because they tried to access a DOM object before a web page had completely loaded. 其他人也遇到了类似的错误,通常是因为他们试图在网页完全加载之前访问DOM对象。 I am doing something different, but I may also have a timing problem. 我正在做一些不同的事情,但是我可能还会遇到时间问题。

I have a Javascript application that communicates with CGI scripts with forms and iframes. 我有一个Javascript应用程序,可以与带有表单和iframe的CGI脚本进行通信。 The first time the user clicks a submit button, the JS application creates an invisible iframe and sends the form's data to the server. 用户首次单击提交按钮时,JS应用程序将创建一个不可见的iframe,并将表单的数据发送到服务器。 The server is supposed to respond by sending data to the iframe which the JS application reads and interprets. 服务器应该通过向JS应用程序读取和解释的iframe发送数据来做出响应。

Here is a very simplified version of the code: 这是代码的非常简化的版本:

var iframe = document.createElement("iframe");
iframe.name = "myIframe";
document.body.appendChild(iframe);
var iframeDocument = 'contentDocument' in iframe? iframe.contentDocument : iframe.contentWindow.document;
iframeDocument.body.innerHTML = "";

The full error is: TypeError: Unable to set value of the property 'innerHTML': object is null or undefined 完整的错误是:TypeError:无法设置属性'innerHTML'的值:对象为null或未定义

The entire page loaded long ago--when the code above executes, the user has entered some data and hit a submit button. 很久以前加载的整个页面-执行上面的代码时,用户已经输入了一些数据并点击了提交按钮。 However, the iframe is new. 但是,iframe是新的。

The code works fine on my computer--I cannot reproduce the problem. 该代码在我的计算机上可以正常工作-我无法重现该问题。 I can see from a log file on the server that the user agent is 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'. 从服务器上的日志文件中可以看到用户代理为“ Mozilla / 5.0(兼容; MSIE 9.0; Windows NT 6.1; Trident / 5.0)”。

Perhaps naively, I thought that the iframe had been created and I could set its document's innerHTML property. 也许天真地,我认为已经创建了iframe,并且可以设置其文档的innerHTML属性。 Could this be a timing problem? 这可能是时间问题吗? Do I have to wait before setting innerHTML, or is something else going wrong? 设置innerHTML之前我是否需要等待,否则是否有其他问题?

If the error is indeed caused by a timing problem, I can probably fix it by creating the iframe when the page first loads. 如果错误确实是由计时问题引起的,那么我可能可以通过在页面首次加载时创建iframe来解决该错误。 If the user then clicks a cancel button, the iframe will never be used--I thought it was better to create the iframe only when it is needed. 如果用户单击取消按钮,则将永远不会使用iframe,我认为最好仅在需要时创建iframe。 To really fix this error, I have to understand it--that is why I am asking my question. 要真正解决此错误,我必须理解它-这就是为什么我问我的问题。

Maybe it's a timing issue, you can try : 可能是时间问题,您可以尝试:

var iframe = document.createElement("iframe"),
    iframeDocument;
iframe.name = "myIframe";

iframe.onload = function(){
    iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
    iframeDocument.body.innerHTML = "";
};

document.body.appendChild(iframe);    

That way you'll be sure that your iframe will be ready 这样一来,您就可以确定自己的iframe已准备就绪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM