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

前端 未结 16 2332
攒了一身酷
攒了一身酷 2020-11-22 00:28

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

16条回答
  •  你的背包
    2020-11-22 00:58

    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.

提交回复
热议问题