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

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

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

I am writing an iframe based facebook app. Now I want to use the same html page to render the normal website as well as the canvas page within facebook. I want to know if I can determine whether the page has been loaded inside the iframe or directly in the browser?

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

Browsers can block access to window.top due to same origin policy. IE bugs also take place. 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.