Js redirect if not in fb iframe

I want to redirect all traffic to my Facebook application tab on my server directly to my Facebook application. Therefore, I check the following code if the user is inside a Facebook iframe or on my web page:

<!-- language: lang-js --> function referrerIsFacebookApp() { if(document.referrer) { return document.referrer.indexOf("facebook.com") !== -1; } return false; } if (!referrerIsFacebookApp()) { top.location.replace("https://www.facebook.com/bommelME/app_264697733636385"); }; 

If I open the page in the browser, everything will work as it should. But if I link to this page and open the link, the redirect does not work. Any clues?

+4
source share
2 answers

Use window.top to determine if your application is in an iFrame or not. Try it.

 if (window!=window.top) { /* I'm in a frame! */ window.location = "https://www.facebook.com/bommelME/app_264697733636385"; } 

Greetings.

+7
source

I think you should check the window.parent object instead of document.referrer , because the page can be referenced by another, as you said, but not included through the iframe

+1
source

All Articles