admin管理员组文章数量:1245206
I have tried many properties of window to see if a page in an iframe can tell if it is in an iframe. I have tried:
if(top.location!= self.location) //doesn't work in Google Chrome
alert("I am in an iframe!")
And this doesn't work (works on all browsers but Chrome). I am writing a userscript for Firefox and Chrome but Chrome really doesn't behave. Is there a way to tell if Chrome can detect if its page is in an iframe?
I have tried many properties of window to see if a page in an iframe can tell if it is in an iframe. I have tried:
if(top.location!= self.location) //doesn't work in Google Chrome
alert("I am in an iframe!")
And this doesn't work (works on all browsers but Chrome). I am writing a userscript for Firefox and Chrome but Chrome really doesn't behave. Is there a way to tell if Chrome can detect if its page is in an iframe?
Share Improve this question asked Mar 11, 2011 at 21:39 user654628user654628 1,4592 gold badges18 silver badges37 bronze badges 2- jsfiddle/hpVec It seems to be working fine in Chrome. – Peter Olson Commented Mar 11, 2011 at 21:45
- 1 var in_iframe = (window.location != window.parent.location); – Prashant Pugalia Commented Jul 12, 2012 at 8:59
2 Answers
Reset to default 15This works for frames I would assume it also works with iFrames
if (top === self) {
// no frame
} else {
//frame
}
window.frameElement
is supported in even the most ancient browsers. It identifies an iframe, embed, or object in which the current window is embedded.
if(!!window.frameElement){
//code to be executed if we are in an iframe
}
More here.
本文标签: javascriptCheck if page is in Iframe for Google ChromeStack Overflow
版权声明:本文标题:javascript - Check if page is in Iframe for Google Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740215434a2242774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论