How to detect that users are opening my Facebook application in an iframe or direct URL

Lets say my Facebook application URL is http://fb.domain.com/ and the application URL is http://apps.facebook.com/demoapp/

Now I want the user to not be able to directly open the application URL and should in the iframe on Facebook.

How to identify and redirect a link to http://apps.facebook.com/demoapp/ if the user opens the link directly?

Tell me the key.

+4
source share
2 answers

If you are just trying to determine if the user has opened your URL inside an iframe, with javascript enabled, you can:

 if (window!=window.top) { /* I'm in a frame! */ } 

Hope that answers your question.

+10
source

Checking for if (window!=window.top) only indicates whether the application is open in the iframe or not. However, this does not mean that it is a FaceBook iframe. When the application opens in the FaceBook iframe (for example, http://apps.facebook.com/demoapp/ ), FaceBook sends the signed_request parameter. If this parameter is valid, you can safely assume that the request comes from a FaceBook iframe. If you do not redirect to the application page using:

 <script type="text/javascript"> top.location.href = "http://apps.facebook.com/demoapp/"; </script> 
+8
source

Source: https://habr.com/ru/post/1342398/


All Articles