陈斌彬的技术博客

Stay foolish,stay hungry

top.location判断自身是否是顶级窗口

如果要在js中来判断自身是否是顶级窗口我们可直接利用top.location!=self.location来加以操作,具体看下面代码

顶级窗口的地址:top.location

本窗口的地址:self.location

if(top.location!=self.location){
top.location = self.location;
}

具体实现程序

if (window.top !== window.self) { // are you trying to put self in an iframe?
  try {
    if (window.top.location.host) { // this is illegal to access unless you share a non-spoofable document domain
      // fun times
    } else {
      bust(); // chrome executes this
    }
  } catch (ex) {
    bust(); // everyone executes this
  }
}