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

查看:22
本文介绍了如何识别网页是在 iframe 中加载还是直接加载到浏览器窗口中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

由于 同源政策.IE 错误也会发生.这是工作代码:

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;
    }
}

topself 都是 window 对象(与 parent 一起),所以你会看到您的窗口是顶部窗口.

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

这篇关于如何识别网页是在 iframe 中加载还是直接加载到浏览器窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆