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

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

问题描述

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

解决方案

浏览器可以由于 window.top rel = noreferrer>同源地政策。 IE也发生错误。以下是工作代码:

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

top self 都是窗口对象(以及),所以你看到你的窗口是最上面的窗口。


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?

解决方案

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.

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

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