13

im building facebook app as a iframe app in fan page. My problem at the moment is next: i added facebook request dialog (http://developers.facebook.com/docs/reference/dialogs/requests), and everything goes well except for one thing: when a user gets notification, the links goes to canvas page, not to fan page (where i would like to go...)

Since i cant convince facebook to add some funcionality (that would be great), im looking for a way to automaticly redirect from app canvas page to fan page, where this app is added as iframe tab.

I hope somebody understands what i want to do... :)

thanks, Peter

doppelgreener
  • 4,809
  • 10
  • 46
  • 63
Peter
  • 728
  • 3
  • 16
  • 34

4 Answers4

31

I also wanted to make sure that users view my Facebook app via a Facebook Page Tab (rather than via the Facebook App page or by directly viewing the actual site itself). I have managed to achieve it with this Javascript in a <script> tag in the head of my document (tested Mac/PC FF,Chrome,Opera,IE6-8,Safari).

<script type="text/javascript">
  function NotInFacebookFrame() {
    return top === self;
  }
  function ReferrerIsFacebookApp() {
    if(document.referrer) {
      return document.referrer.indexOf("apps.facebook.com") != -1;
    }
    return false;
  }
  if (NotInFacebookFrame() || ReferrerIsFacebookApp()) {
    top.location.replace("http://permalink-to-my-facebook-page-tab");
  }
</script>
sakibmoon
  • 2,026
  • 3
  • 22
  • 32
stephen
  • 1,200
  • 1
  • 13
  • 16
14

If I understand you correctly, you can do this:

top.location.href='http://www.facebook.com/YOUR_PAGE_NAME?sk=app_YOUR_APP_ID'

or you can use a header to redirect to that url. This will redirect to the application's tab on your fan page.

sakibmoon
  • 2,026
  • 3
  • 22
  • 32
DannyKK
  • 971
  • 12
  • 16
  • 1
    I am facing the same issue and I was wondering how you guys detect that you are on the app page and not the canvas url. How do you prevent an infinite redirection loop? – Michael Copeland Apr 19 '11 at 14:25
  • 1
    @Michael. Well, you usually make the tab url and page url separate, or add a GET parameter in the application settings to know which page was called. – DannyKK Apr 19 '11 at 14:49
  • 2
    @DannyKK. Thanks for the tip. I found an alternate way to do this without having to set a GET variable as well. You can also check the $_SERVER['HTTP_REFERER'] variable to see if _apps.facebook.com_ is there. – Michael Copeland Apr 19 '11 at 20:41
  • Yes, i also do it like this. But im facing another problem now: When i have apps.facebook.com in HTTP_REFERER, i do: top.location.href='http://www.facebook.com/YOUR_PAGE_NAME?sk=app_YOUR_APP_ID' - its working fine in mozzila and chrome, but in internet explorer i get "permission denied". I figured out its a cross domain scripting, but what is a workaround? does anybody have the same problem? – Peter Apr 21 '11 at 08:25
  • Sorry, i was doing with: top.window.location, when i changed to top.location.href it is working :) – Peter Apr 21 '11 at 08:40
  • You can also tell if you are in your page tab by decoding the signed_request from Facebook and seeing if it contains page= – mtjhax Feb 24 '12 at 21:18
2

My application is quite dynamic and I never know which page it's added to. So when I used URL 'http://www.facebook.com/YOUR_PAGE_NAME?sk=app_YOUR_APP_ID' it only redirected me to page, but not to application tab. What I do is:

1) I get page id from signed_request (the encrypted in base64url parameter that your app gets once someone comes to the page, so you have to decrypt it and pick id value from JSON page object)

2) Then I get page data from https://graph.facebook.com/PAGE_ID_YOUR_GET. You get JSON object with some page data in response in JSON format.

3) Only after point 2 I get 'link' value of page from response. It's like http://www.facebook.com/pages/Some-Page-Name

4) And finally I add '?sk=app_YOUR_APP_ID' to page link.

Maybe that's too complicated, but that's the only way it worked for me the way I expected (redirecting exactly to page application tab).

Anton M
  • 100
  • 6
  • 1
    This will NOT WORK if user go to your app directly (canvas), there will not be a page object in signed_request, so either way you must set your page name and id in every instance of app config to do a redirect to the right place. – Programista Jul 25 '12 at 12:37
-2
<script type="text/javascript">
  function NotInFacebookFrame() {
    return top === self;
  }
  function ReferrerIsFacebookApp() {
    if(document.referrer) {
      return document.referrer.indexOf("apps.facebook.com") == -1;
    }
    return false;
  }
  if (NotInFacebookFrame() || ReferrerIsFacebookApp()) {
    top.location.replace("http://permalink-to-my-facebook-page-tab");
  }
</script>