javascript 从 iframe 更改顶部窗口位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6973199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:32:09  来源:igfitidea点击:

Change top window location from iframe

phpjavascripthtmlfacebook

提问by Immo

Hello I want to redirect my Facebook App page to another app page using php and javascript

您好,我想使用 php 和 javascript 将我的 Facebook 应用页面重定向到另一个应用页面

I have

我有

echo "<script type='text/javascript'>
if(window.top.location.href.toLowerCase().indexOf('http://apps.facebook.com/app1/') != -1) {
    window.top.location.href = 'http://apps.facebook.com/app2/2/';
    alert("Done");
}
else
   alert(window.top.location.href); 
</script>";

But I get an error:

但我收到一个错误:

Unsafe JavaScript attempt to access frame with URL http://apps.facebook.com/myapp/?ref=bookmarksfrom frame with URL http://mydomain.com/?ref=bookmarks. Domains, protocols and ports must match.

Unsafe JavaScript attempt to access frame with URL http://apps.facebook.com/myapp/?ref=bookmarksfrom frame with URL http://mydomain.com/?ref=bookmarks. Domains, protocols and ports must match.

Is it possible to redirect from the Facebook Application iFrame without any problem?

是否可以毫无问题地从 Facebook 应用程序 iFrame 重定向?

回答by Peavey

due to XSS-resrictions you can set but cannot read top.location.href if your iframe resides on another domain than the page that calls the iframe. If the page you are redirecting to has the same domainname you will be able to check top.location.href. So the logic if your code should be reversed checking if it can access top.location.href and if NOT redirect. put it in a try..catch block to avoid javascript errors when accessing top.location

由于 XSS 限制,如果您的 iframe 位于调用 iframe 的页面之外的另一个域中,您可以设置但无法读取 top.location.href。如果您重定向到的页面具有相同的域名,您将能够检查 top.location.href。因此,如果您的代码应该反向检查它是否可以访问 top.location.href 以及是否不能重定向,则逻辑。将它放在 try..catch 块中以避免访问 top.location 时出现 javascript 错误