Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

js redirect if not in fb iframe

i want to redirect all traffic going to my Facebook app tab on my server directly to my Facebook app. Therefore I check with the following code if the user is inside the Facebook iframe or on my webpage:

<!-- 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 up the page with the browser everything works as it should. But if I link to this page and open the link the redirect doesnt work. Any hints?

like image 247
fourgood Avatar asked Feb 19 '23 19:02

fourgood


2 Answers

Use window.top to detect whether your app is in an iFrame or not. Try this.

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

Cheers.

like image 109
mukama Avatar answered Feb 28 '23 15:02

mukama


I think you should check the window.parent object instead of document.referrer, because the page can be referenced from another one as you've said but not being included via iframe

like image 33
haynar Avatar answered Feb 28 '23 14:02

haynar