简体   繁体   中英

How do I detect either users open my Facebook app in iframe or direct url

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

Now I want a user cannot open the application url directly and must in iframe on Facebook.

How do I detect & redirect link to http://apps.facebook.com/demoapp/ if user open the link directly?

Let me know the key.

If you are just trying to detect whether a user has opened your URL inside of an iframe, with javascript enabled you could do:

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

Hope that answers your question.

Checking for if (window!=window.top) tells you only whether you application is opened in iframe or not. However it does not tell if it the FaceBook iframe. When application is opened in FaceBook iframe (for example http://apps.facebook.com/demoapp/ ) FaceBook sends signed_request parameter. If this parameter is valid you can safely assume request comes from FaceBook iframe. If not redirect to correct application page with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM