检查页面是否在Google Chrome的Iframe中

use*_*628 6 javascript iframe google-chrome window

我尝试了很多窗口属性,看看iframe中的页面是否可以判断它是否在iframe中.我试过了:

if(top.location!= self.location) //doesn't work in Google Chrome
   alert("I am in an iframe!")
Run Code Online (Sandbox Code Playgroud)

这不起作用(适用于除Chrome之外的所有浏览器).我正在为Firefox和Chrome编写用户脚本,但Chrome确实没有表现.有没有办法判断Chrome是否可以检测其网页是否在iframe中?

Joh*_*ock 15

这适用于帧,我认为它也适用于iFrames

if (top === self) { 
  // no frame
} else { 
  //frame 
}
Run Code Online (Sandbox Code Playgroud)