javascript - 使用内容跨源创建 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();
解决方案
似乎“沙盒”实际上确实阻止了对文档的访问。删除“沙盒”解决了这个问题。
推荐阅读
- javascript - 填写字段+活动复选框时,我们激活按钮
- firebase - Firestore RESTful API 查询仅返回 readTime 作为有效负载
- javascript - Rails 远程和 jquery ajaxStop
- javascript - 将数组的内容作为列表元素添加到无序列表
- javascript - 在 Javascript 中选择数组时遇到问题
- javascript - 单击按钮时如何获取类的索引号
- javascript - React 中的随机集固定大小
- undefined - 使用 panelvar 包选择的未定义列
- python - HEALPy:在购物车视图旋转后获取数组的新索引
- swift - 播放提醒声音 SwiftUI