如何在沙盒iframe(IE11)中使用javascript创建iframe内容?

use*_*899 6 html javascript iframe internet-explorer sandbox

我正在尝试通过创建iframe并使用javascript或vbscript动态构建iframe内容来构建用于Internet Explorer的测试页面.我通常会使用数据:URI,但IE会阻止它.

例.

<iframe sandbox="allow-scripts" src="javascript:document.write('test')"></iframe>
Run Code Online (Sandbox Code Playgroud)

看来IE是唯一一个不允许我通过javascript:function()src构建iframe内容的浏览器,即使设置了allow-scripts sandbox属性.我不是试图在iframe和父窗口之间传递任何信息,并且不希望设置allow-same-origin,因为它几乎会破坏沙盒iframe的目的.

是否还有其他方法可以动态构建除javascript或数据之外的iframe内容:src中的URI,或者通过父窗口中的javascript,因为由于相同的原始限制,它不能与沙盒iframe一起使用?我也不想从外部页面设置内容.

小智 1

javascript:是一种奇怪的URI协议。它适用于某些上下文,例如<a href>,但不是所有上下文 - 例如,窗口的位置不能设置为这样的 URI。(虽然您可以分配一个javascript:URI 作为window.location运行脚本的真正迂回方式,但窗口的位置不会保持设置为该值。)

要将内容写入 IFRAME,请获取对框架文档的引用并向其写入。这样做需要您设置allow-same-origin沙箱标志。

<iframe id="myframe" sandbox="allow-scripts allow-same-origin" src="about:blank"></iframe>

var frame = document.getElementById("myframe");
var fdoc = frame.contentDocument;

fdoc.write("Hello world"); // or whatever
Run Code Online (Sandbox Code Playgroud)

实例: http: //jsfiddle.net/wUvrF/1/

  • javascript:可以在所有浏览器中用作 iFrame 的 src 属性中的 URI,只要未设置 sandbox 属性或在除 IE 之外的所有浏览器中将 sandbox 属性设置为allow-scripts 或在 IE 中设置为allow-same-origin。看来这是一个同源问题,因为沙盒 iframe 是一个独特的域,这将使我无法通过父文档上的脚本对其进行修改。在沙箱属性中将 iFrame 设置为 allowed-same-origin 违背了对 iFrame 进行沙箱处理的目的,因此对于我要测试的内容来说它不是一个可行的选项。 (2认同)