Javascript 更改父窗口';来自IFrame的URL

Javascript 更改父窗口';来自IFrame的URL,javascript,html,security,iframe,Javascript,Html,Security,Iframe,我的情况是,我在两个不同的服务器上有web应用程序,其中App1在IFrame中包含App2。App2中的任何链接都可以具有target=“\u parent”属性,允许在顶部窗口中打开这些链接。但是,我找不到任何方法在Javascript中获得相同的行为。我发现,它声称子框架可以使用parent.foo()调用父框架上的javascript,但这在IE8或FF3.5中似乎不起作用。我发现这解释了这个安全模型的工作原理。但奇怪的是,我不能在Javascript中使用简单的,但(据我所知)这只能在

我的情况是,我在两个不同的服务器上有web应用程序,其中App1在IFrame中包含App2。App2中的任何链接都可以具有
target=“\u parent”
属性,允许在顶部窗口中打开这些链接。但是,我找不到任何方法在Javascript中获得相同的行为。我发现,它声称子框架可以使用
parent.foo()
调用父框架上的javascript,但这在IE8或FF3.5中似乎不起作用。我发现这解释了这个安全模型的工作原理。但奇怪的是,我不能在Javascript中使用简单的<代码>,但(据我所知)这只能在Firefox中使用


例子 server1/test.html

函数myCallback(foo){
警报(foo);
}
server2/test2.html

函数clickit(){
parent.document.location=”http://www.google.com“;//不允许
parent.myCallback(“http://www.google.com“”;//不允许
}
这应该在一个iFrame中


没有,而且理由充分。如果需要,则必须通过两台服务器中的一台运行所有通信;例如,让server1充当“server2/test2.html”所有请求的代理。如果父级和iframe都位于同一域下的子域上,则可以使用
document.domain
属性执行某些操作。如果车身和iframe被视为来自同一个原点,则可以更改位置;我自己没试过。一些阅读

如果帧在同一个域上,您应该能够访问父帧。否则不,这是安全问题

想到的唯一解决方法是使用AJAX更新每个服务器上的文件,然后检查服务器端的相反文件的内容。如果允许来自外部域的连接,则可以使用单个数据库执行相同的操作


不过,如果你只需在框架中弹出一个链接并告诉用户点击它继续,这就有点过分了。

好吧,我做了更多的调查,似乎postMessage在所有现代浏览器中都能正常工作,甚至IE(需要注意的是,IE的方式略有不同)。下面是我如何让它工作的(在IE8、FF3.5、Chrome 3.0、Safari 4 beta、Opera 9.64的WinXP上测试):

server1/test.html

如果(navigator.appName==“Microsoft Internet Explorer”)
attachEvent(“onmessage”,receiveMessage);
其他的
window.addEventListener(“消息”,receiveMessage,false);
函数接收消息(e){
如果(例如,原点=”http://server2这对安全很重要
if(e.data.indexOf('redirect:')==0)
document.location=e.data.substr(9);
}
server2/test2.htm

函数clickit(){
parent.postMessage('重定向:http://www.google.com', 'http://server1');
}
这应该在一个iFrame中


您可以做的一件简单的事情是: 从iframe页面的JavaScript代码执行以下命令

top.location = "https://www.google.co.in/";
这会将窗口URL的位置更改为
https://www.google.co.in/

还有一件事——当你不想让任何人破坏你的网站时,这种策略也很有用
只需在document ready部分中编写上述代码。

top.location.href=”“对我有效,谢谢此解决方案无效。Chrome正在阻止重定向。
<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>
<html>
<head>
<script type="text/javascript">
if(navigator.appName == "Microsoft Internet Explorer")
  window.attachEvent("onmessage", receiveMessage);
else
  window.addEventListener("message", receiveMessage, false);

function receiveMessage(e) {
  if(e.origin == "http://server2") //important for security
    if(e.data.indexOf('redirect:') == 0)
      document.location = e.data.substr(9);
}
</script>
</head>
<body>
<iframe src="http://server2/test2.htm" width="400" height="150"></iframe>
</body>
</html>
<html><body>
<script>
function clickit() {
  parent.postMessage('redirect:http://www.google.com', 'http://server1');
}
</script>
<p>This should be in an iFrame!</p>
<p><a href="http://www.google.com" target="_parent">normal link</a></p>
<p><a href="javascript:clickit()">javascript link</a></p>
</body></html>
top.location = "https://www.google.co.in/";