基本上,我想要这样的东西(它是 ES6):
<iframe id="iframe"></iframe>
<script>
var iframeDoc = document.getElementById('iframe').contentDocument;
iframeDoc.open('text/html', 'replace');
iframeDoc.write(`
<!doctype html>
<html>
<body>
<script>
// do not allow window.parent access!
alert(window.parent.document.cookie);
<\/script>
</body>
</html>
`);
iframeDoc.close();
</script>
警告框显示父窗口的 cookie,这是不好的。不过,我希望 iframe 执行自己的沙盒 javascript 代码。这在 HTML5 中是否可行?
我已经尝试了
sandbox
的所有排列属性及其值。可以构建 iframe 并将其内容作为
src
的数据 URI。属性,但这似乎有点hacky。 (或者可能没有?URI 长度没有限制吗?)
最佳答案
您是否尝试过使用 sandbox
?
W3C 对此有很好的文档,它会阻止顶级浏览上下文。
http://www.w3schools.com/tags/att_iframe_sandbox.asp
另请查看本教程:
http://www.html5rocks.com/en/tutorials/security/sandboxed-iframes/
关于javascript - 如何防止 iframe 访问父窗口但允许父窗口访问 iframe 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35666548/