Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

javascript - Redirect from Facebook canvas page to website

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:

<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?

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.

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:

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

how about using the following code:

 <script type='text/javascript'>
    top.location.href = 'http://www.yousite.com';
 </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...