【发布时间】:2012-10-26 05:09:38
【问题描述】:
基本上我想要一个 iFrame,它总是限制它的内容,就好像它来自不同的域一样,即使内容来自同一个来源。
有什么办法吗?
【问题讨论】:
标签: javascript iframe same-origin-policy
基本上我想要一个 iFrame,它总是限制它的内容,就好像它来自不同的域一样,即使内容来自同一个来源。
有什么办法吗?
【问题讨论】:
标签: javascript iframe same-origin-policy
最好的解决方案可能是在 iframe 上使用 HTML5 沙盒属性,它(默认情况下)显式禁用脚本和对父 DOM 的同源访问。
http://msdn.microsoft.com/en-us/hh563496.aspx的好介绍
截至 2012 年 12 月,这似乎是 supported on most current browsers。
【讨论】:
这将在子框架/窗口中隐藏window.parent,但不会隐藏top 属性。
但是在子窗口/框架的 onload 事件结束之前,window.parent 属性仍然可以访问。
<html>
<head>
<style type="text/css">
#wrapper {width:1000px;height:600px;}
</style>
<script type="text/javascript">
window.onload = function() {
var frm = document.getElementById('childFrame');
var win = frm.contentWindow || (frm.contentDocument && frm.contentDocument.parentWindow) || (frm.document && frm.document.parentWindow);
if (win) win.parent = null;
}
</script>
</head>
<body>
<div id="wrapper">
<iframe id="childFrame" src="child.html" frameborder="0" style="width:100%;height:100%;"></iframe>
</div>
</body>
</html>
【讨论】:
top,与parent不同,不能重新定义。