javascript 从 Facebook 画布页面重定向到网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7157825/
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
Redirect from Facebook canvas page to website
提问by at.
Is there a way to redirect from my Facebook canvas page to my external website? I've seen blogs and stackoverflow answers of the form:
有没有办法从我的 Facebook 画布页面重定向到我的外部网站?我看过以下形式的博客和 stackoverflow 答案:
<script>
window.top.location = 'http://www.yoursite.com/';
</script>
Unfortunately these kinds of solutions never work. I've tried top.location.href, window.location and location.href as well.. nothing works. Did Facebook remove the possibility to redirect from javascript by parsing out this kind of code? Is there a way to still redirect?
不幸的是,这些解决方案从未奏效。我也试过 top.location.href、window.location 和 location.href ......没有任何效果。Facebook 是否通过解析这种代码消除了从 javascript 重定向的可能性?有没有办法仍然重定向?
UPDATE: What happens is the webpage acts like there was never a javascript redirect instruction. And in fact, when I look at the source code through firebug I see no javascript redirects.
更新:发生的情况是网页行为就像从来没有 javascript 重定向指令一样。事实上,当我通过 firebug 查看源代码时,我看不到 javascript 重定向。
UPDATE 2: I get the following javascript errors form my canvas page when I try window.top.location = 'http://www.yoursite.com/';
and the other variations above. It seems Facebook doesn't allow access to the window, top or location global javascript variables:
更新 2:当我尝试window.top.location = 'http://www.yoursite.com/';
上述其他变体时,我的画布页面出现以下 javascript 错误。Facebook 似乎不允许访问 window、top 或 location 全局 javascript 变量:
Uncaught ReferenceError: a217374027657_location is not defined
Uncaught ReferenceError: a217374027657_window is not defined
Uncaught ReferenceError: a217374027657_top is not defined
Interestingly, document.location = 'http://www.yoursite.com/';
works in that there are no exceptions... but of course the webpage doesn't get redirected.
有趣的是,document.location = 'http://www.yoursite.com/';
在没有例外的情况下有效......但当然网页不会被重定向。
UPDATE 3: Figured it out! Javascript redirect only works with iframe canvases. I had to change my settings in the advanced screen to use iframe instead of FBML.
更新3:想通了!Javascript 重定向仅适用于 iframe 画布。我不得不更改高级屏幕中的设置以使用 iframe 而不是 FBML。
采纳答案by Eddy Chan
how about using the following code:
如何使用以下代码:
<script type='text/javascript'>
top.location.href = 'http://www.yousite.com';
</script>
回答by Sean Tomlins
Beware, redirecting the user to an external site can violate the platform policy
请注意,将用户重定向到外部站点可能会违反 平台政策
The primary purpose of your Canvas or Page Tab app on Facebook must not be to simply redirect users out of the Facebook experience and onto an external site.
Facebook 上的 Canvas 或 Page Tab 应用程序的主要目的不能是简单地将用户从 Facebook 体验重定向到外部站点。
回答by Matt Kaye
I just did this on my app:
我刚刚在我的应用程序上做了这个:
if(window.top.location.href != window.location.href){ window.top.location.href = window.location.href; }只需将其放在画布页面的顶部即可。如果顶部 href 与提取画布内容的 url 不匹配,它会重定向到您的站点。
回答by genesis
From my script
从我的脚本
<script type='text/javascript'>
top.location.href = 'http://riseofkings.net/fb.php?setcook&cook=cookhere';
</script>
And it always worked. Does it throw any error, exception or so?
它总是有效。它是否抛出任何错误、异常等?
If you can use PHP, try to die()
after this javascript code
如果您可以使用 PHP,请尝试使用die()
此 javascript 代码
回答by TJHeuvel
This is not possible, Facebook disabled this functionality on purpose. You'd either have to stay in your canvas, open a popup (if possible) or just create a link for the user to click on.
这是不可能的,Facebook 故意禁用了此功能。您要么必须留在画布中,打开一个弹出窗口(如果可能),要么只创建一个供用户点击的链接。