<iframe>使浏览器永久运行

时间:2019-01-05 22:17:11

标签: javascript html iframe

这是我的文件:

  <!DOCTYPE html>

  <头>
    
     iframe测试
  
  <身体>
    
    <脚本>
    document.addEventListener(“ DOMContentLoaded”,function(){
      let content =“ 

你好世界

”;       document.getElementById(“ frm”)。contentDocument.write(content);     });        

当我在Firefox或Chrome上打开它时,它可以工作,但浏览器会无限期运行。我该如何解决这个问题?

谢谢您的帮助。

1 个答案:

答案 0 :(得分:2)

const content = "<h1>Hello world</h1>";
const frm = document.getElementById("frm");
//for cross-browser compatibility
const frmCnt = frm.contentWindow || frm.contentDocument.document || frm.contentDocument;
//needed to write inside the already generated <html></html> tags of the iframe
const frmDoc = frmCnt.document;
frmDoc.open();
frmDoc.write(content);
frmDoc.close();