相关疑难解决方法(0)

从IFrame更改父窗口的URL

我有一种情况,我在两个不同的服务器上有Web应用程序,其中App1在IFrame中包含App2.App2中的任何链接都可以具有target="_parent"属性,允许在顶部窗口中打开这些链接.但是,我找不到任何方法在Javascript中获得相同的行为.我找到了这个页面,声称子框架可以使用父框架调用javascript parent.foo(),但这似乎在IE8或FF3.5中不起作用.我找到了这个问题,解释了这个安全模型的工作原理.但似乎很奇怪,我不能用Javascript做一些简单的<a>标签. 这有什么解决方法吗?我知道window.postMessage,但(据我所知)这只适用于Firefox.


服务器1/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>
Run Code Online (Sandbox Code Playgroud)

服务器2/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>
Run Code Online (Sandbox Code Playgroud)

html javascript security iframe

14
推荐指数
2
解决办法
2万
查看次数

标签 统计

html ×1

iframe ×1

javascript ×1

security ×1