我正在尝试弄清楚是否可以通过编程方式更改 iFrame 的沙箱值。
来自 MDN iframe :
When the embedded document has the same origin as the main page, it is strongly discouraged to use both allow-scripts and allow-same-origin at the same time, as that allows the embedded document to programmatically remove the sandbox attribute. Although it is accepted, this case is no more secure than not using the sandbox attribute.
但是,更改父项中的属性并不会像在子项中暗示的那样成功触发沙盒。
document.getElementById('myFrame').setAttribute('sandbox', 'allow-scripts');
在该 iFrame 中加载的页面可以成功获得对父级的访问权限,如果沙盒正在运行则不会出现这种情况。属性确实发生了变化,但安全性存在缺陷,因为它不会阻止对专利的访问。
console.log(window.parent.document);
以上内容仍然适用于“以编程方式”启用沙箱的 iFrame。 Chrome 和 Firefox 都是这种情况,这意味着要么是预期的未记录的功能,要么是实现不当的规范。
有人对什么是适当的期望有任何想法吗?
最佳答案
请试试这个 fiddle :http://jsfiddle.net/yAwxy/
当脚本运行时它不工作 onLoad
b/c iframe 已经在工作。
如果您尝试 onDomready
,则应用沙盒 b/c 脚本尚未运行。
如果您使用此 fiddle 登录:
<iframe id="myFrame" srcdoc="<script>console.log('Executing script inside iFrame')</script>">
</iframe>
在脚本中
console.log('Executing script inside page')
当脚本 console.log('Executing script inside page')
被包装在 onLoad
时,输出是:
Executing script inside iFrame
Executing script inside page
当包装 onDomready
时:
Executing script inside page
Executing script inside iFrame
另见 http://jsfiddle.net/yAwxy/1/ .
所以要改变规则,他们必须改变OndomReady
关于javascript - 切换 iFrame 沙盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21652320/