我正在写一个基于iframe的facebook应用程序。现在我想使用相同的html页面来渲染正常的网站以及facebook内的画布页面。我想知道我是否可以确定页面是否已经在iframe内加载或直接在浏览器中加载?


当前回答

我不确定这个例子如何适用于旧的Web浏览器,但我使用这个IE, Firefox和Chrome没有问题:

var iFrameDetection = (window === window.parent) ? false : true;

其他回答

在每一页写这个javascript

if (self == top)
  { window.location = "Home.aspx"; }

然后它会自动重定向到主页。

我以前经常检查窗户。父键对我有用,但最近window是一个循环对象,总是有父键,不管有没有iframe。

正如评论所言,很难与窗口相比。父母工作。不确定这是否会工作,如果iframe是完全相同的网页作为父母。

window === window.parent;

如果你想知道用户是否通过facebook page tab或canvas访问你的应用,请检查Signed Request。如果你没有得到它,可能用户没有从facebook访问。 确保确认signed_request字段结构和字段内容。

使用php-sdk,你可以像这样获得Signed Request:

$signed_request = $facebook->getSignedRequest();

你可以在这里阅读更多关于签名请求的内容:

https://developers.facebook.com/docs/reference/php/facebook-getSignedRequest/

在这里:

https://developers.facebook.com/docs/reference/login/signed-request/

function amiLoadedInIFrame() {
    try {
         // Introduce a new propery in window.top
         window.top.dummyAttribute = true;
         // If window.dummyAttribute is there.. then window and window.top are same intances
         return !window.dummyAttribute;
    } catch(e) {
         // Exception will be raised when the top is in different domain
         return true;
    }
}

我用这个:

var isIframe = (self.frameElement && (self.frameElement+"").indexOf("HTMLIFrameElement") > -1);