如何识别网页是否正在iframe中加载或直接进入浏览器窗口? [英] How to identify if a webpage is being loaded inside an iframe or directly into the browser window?
本文介绍了如何识别网页是否正在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屋!
查看全文