首页 > 解决方案 > 使用内容跨源创建 iFrame

问题描述

我正在尝试使用javascript创建一个空的iframe,用一些html代码(这里:“Hello World”)填充它。该脚本文件托管在域中a.com,并从位于b.net.

所有其他 js 代码都可以工作,但是当我尝试设置 iframe 的内容时,出现跨源错误。是否有合适的解决方法有效?

var node = document.createElement("iframe");
node.setAttribute("border", "0px");
node.setAttribute("sandbox", "");
node.setAttribute("id", "preload");
var obj = document.getElementsByTagName("footer")[0].appendChild(node);
node.contentWindow.document.open();
node.contentWindow.document.write("Hello World");
node.contentWindow.document.close();

标签: javascriptiframexss

解决方案


似乎“沙盒”实际上确实阻止了对文档的访问。删除“沙盒”解决了这个问题。


推荐阅读