最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - js redirect if not in fb iframe - Stack Overflow

programmeradmin0浏览0评论

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") !== -1;
    }
    return false;
}

if (!referrerIsFacebookApp()) {
    top.location.replace("");
};

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?

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.") !== -1;
    }
    return false;
}

if (!referrerIsFacebookApp()) {
    top.location.replace("https://www.facebook./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?

Share Improve this question edited Jul 21, 2012 at 9:53 Teun Zengerink 4,4035 gold badges32 silver badges32 bronze badges asked Jul 20, 2012 at 14:28 fourgoodfourgood 1873 silver badges15 bronze badges 2
  • it depends on what is inside your referrerIsFacebookApp function – haynar Commented Jul 20, 2012 at 14:31
  • sry, i just edited the code. pasted the wrong one ;) – fourgood Commented Jul 20, 2012 at 14:34
Add a ment  | 

2 Answers 2

Reset to default 7

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./bommelME/app_264697733636385";
}

Cheers.

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

发布评论

评论列表(0)

  1. 暂无评论