Warm tip: This article is reproduced from serverfault.com, please click

javascript-如何识别网页是在iframe中加载还是直接加载到浏览器窗口中?

(javascript - How to identify if a webpage is being loaded inside an iframe or directly into the browser window?)

发布于 2008-11-28 15:45:25

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

Questioner
akshat
Viewed
0
5,752 2015-08-15 03:15:14

window.top由于相同的原始策略,浏览器可能会阻止访问IE错误也会发生。这是工作代码:

function inIframe () {
    try {
        return window.self !== window.top;
    } catch (e) {
        return true;
    }
}

topself都是两个window对象(以及parent),因此你正在查看你的窗口是否是顶部窗口。