Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 577
at. Avatar asked Aug 23 '11 07:08

at.


3 Answers

how about using the following code:

 <script type='text/javascript'>
    top.location.href = 'http://www.yousite.com';
 </script>
like image 96
Eddy Chan Avatar answered Oct 29 '22 01:10

Eddy Chan


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.

like image 5
Sean Tomlins Avatar answered Oct 29 '22 01:10

Sean Tomlins


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

like image 1
genesis Avatar answered Oct 29 '22 00:10

genesis