Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

javascript - 如何识别网页是在iframe中加载还是直接加载到浏览器窗口中?(How to identify if a webpage is being loaded inside an iframe or directly into the browser window?)

I am writing an iframe based facebook app.

(我正在写一个基于iframe的Facebook应用程序。)

Now I want to use the same html page to render the normal website as well as the canvas page within facebook.

(现在,我想使用相同的html页面来呈现普通网站以及facebook中的画布页面。)

I want to know if I can determine whether the page has been loaded inside the iframe or directly in the browser?

(我想知道是否可以确定页面是否已在iframe中加载或直接在浏览器中加载?)

  ask by akshat translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Browsers can block access to window.top due to same origin policy .

(由于相同的原始策略,浏览器可以阻止对window.top访问。)

IE bugs also take place.

(IE错误也会发生。)

Here's the working code:

(这是工作代码:)

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

top and self are both window objects (along with parent ), so you're seeing if your window is the top window.

(topself都是window对象(与parent ),因此您要查看窗口是否是顶部窗口。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...