从 srcdoc 设置内容时的 iframe onload 事件

Ali*_*eit 9 html javascript iframe

如何为从srcdoc属性加载的 iframe 指定 onload 属性。例如:

<iframe id="Content" runat="server" srcdoc="Large HTML contents that are set from the server"></iframe>
Run Code Online (Sandbox Code Playgroud)

我正在使用 asp.net webforms 填充来自另一台服务器的内容。

然而,触发 iframe 加载的正常方法如下,但这不起作用:

<iframe id="Content" runat="server" srcdoc="Large HTML contents that are set from the server"></iframe>
Run Code Online (Sandbox Code Playgroud)

我发现 2018 年 github 上报告了一个问题“load”事件处理程序被过早地调用 iframe.srcdoc

有什么线索吗?

小智 0

我想出的最接近的方法是在 init 之后调用类似的东西。

const interval = setInterval(function() {
      var iFrameID = <HTMLIFrameElement>document.getElementById('Content');

      if(iFrameID) {
        let newheight = iFrameID.contentWindow.document.body.scrollHeight + "px";
        iFrameID.style.height = newheight;

      } 
    }, 200);
Run Code Online (Sandbox Code Playgroud)

这会将 iframe 高度设置为每 2 毫秒与 iframe 内容的高度相匹配。这个函数不会终止,我还没有找到好的停止条件。