【问题标题】:Reload iFrame (same-domain) without changing `src=`在不更改 `src=` 的情况下重新加载 iFrame(同域)
【发布时间】:2018-05-23 16:39:08
【问题描述】:

我正在动态创建 iframe ,它只包含内容(没有 src 属性)。

var iframe = document.createElement('iframe');
iframe.contentWindow.document.body.innerHTML=  my_page_content;  
....

但是,JS 脚本(my_page_content 内的 script src=".....")没有被加载,我必须重新加载脚本。

这些方法无法帮助我重新加载框架:

//this one emptyfies the iframe at all
iframeElement.src = iframeElement.src
//this one is not possible, because the `<script>` links are embedded in the content already.
var script = doc.createElement('script');  script.src = "jquery.min.js";
framebody.appendChild(script);

强制 iframe 在其 html 内容中加载包含的 scripts 的解决方案是什么?也许我以错误的方式附加内容?

【问题讨论】:

  • 脚本是否在它们引用的 html 之后?可疑问题与加载事件有关。创建一个重现问题的minimal reproducible example
  • @charlietfl ,谢谢,我现在找到了解决方案,并在下面发布了。

标签: javascript php jquery iframe frame


【解决方案1】:

我解决这个问题的唯一方法是使用write() 命令在 iframe 中写入正文:

var ifr = iframeElement.contentWindow.document;
ifr.open(); ifr.write("<body>"+ my_page_content +"</body>");    ifr.close();

appendChild().innerHTML 都没有帮助我。

【讨论】:

  • 真的吗?如果可以的话,我认为你最好使用 write() 以外的其他东西,我的意思是不要 appendChild() ,不要 iframeElement.contentWindow.document.body.innerHTML=markeup
  • @user10089632 他们都没有帮助
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2018-02-23
  • 2014-01-10
  • 2021-01-10
  • 2016-02-08
  • 2020-10-04
相关资源
最近更新 更多