8

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.

at.
  • 50,922
  • 104
  • 292
  • 461

5 Answers5

8

how about using the following code:

 <script type='text/javascript'>
    top.location.href = 'http://www.yousite.com';
 </script>
Eddy Chan
  • 1,161
  • 1
  • 8
  • 15
5

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.

Sean Tomlins
  • 365
  • 3
  • 13
1

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

genesis
  • 50,477
  • 20
  • 96
  • 125
  • Does that work on your canvas page right now? Interesting though, I do actually get a javascript exception when I uncomment the top.location.href statement: "Uncaught ReferenceError: a217374027657_top is not defined". No idea what that means... – at. Aug 23 '11 at 09:09
  • @at: I've got no idea, I've moved from this to direct solution EDIT: yes: http://apps.facebook.com/riseofkings_login – genesis Aug 23 '11 at 09:18
  • hmm, when I went to your canvas page, it redirected me to accept your application. I accepted your application and then I was able to be redirected to your website. I wonder if javascript redirects need to be only for people who have accepted your application? – at. Aug 23 '11 at 09:43
  • where did you put that script? I'm just testing with a canvas url of mydomain.com/test.html? That simply responds with the above script and nothing else. Maybe I need to have a `` tag and put the script in there or something to that effect? – at. Aug 23 '11 at 09:44
  • @at: There's nothing but above script on my side – genesis Aug 23 '11 at 09:45
1

I just did this on my app:

    if(window.top.location.href != window.location.href){
    window.top.location.href = window.location.href;
    }
    
Just place that on the top of your canvas page. If the top href doesn't match the url your canvas content is pulled from, it redirects to your site.
Matt Kaye
  • 465
  • 1
  • 5
  • 15
-2

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.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46