fourgood
fourgood

Reputation: 187

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?

Upvotes: 4

Views: 2116

Answers (2)

mukama
mukama

Reputation: 979

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.

Upvotes: 7

haynar
haynar

Reputation: 6030

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

Upvotes: 1

Related Questions