【发布时间】:2010-12-24 01:06:45
【问题描述】:
我有一种情况,我在两台不同的服务器上有 Web 应用程序,其中 App1 在 IFrame 中包含 App2。 App2 中的任何链接都可以具有target="_parent" 属性,该属性允许这些链接在顶部窗口中打开。但是,我找不到任何方法在 Javascript 中获得相同的行为。我发现this page,它声称子框架可以使用parent.foo() 在父框架上调用javascript,但这似乎在IE8 或FF3.5 中不起作用。我找到了this SO question,它解释了这个安全模型是如何工作的。但奇怪的是,我不能用 Javascript 做我可以用简单的<a> 标签做的事情。 有什么解决方法吗?我知道window.postMessage,但是(据我所知)这只适用于Firefox。
示例
server1/test.html
<html>
<head>
<script type="text/javascript">
function myCallback(foo) {
alert(foo);
}
</script>
</head>
<body>
<iframe src="http://server2/test2.htm" width="400" height="150"></iframe>
</body></html>
server2/test2.html
<html><body>
<script>
function clickit() {
parent.document.location = "http://www.google.com"; //not allowed
parent.myCallback("http://www.google.com"); //not allowed
}
</script>
<p>This should be in an iFrame!</p>
<p><a href="http://www.google.com" target="_parent">normal link (works)</a></p>
<p><a href="javascript:clickit()">javascript link</a></p>
</body></html>
【问题讨论】:
标签: javascript html security iframe