代码之家  ›  专栏  ›  技术社区  ›  Tom

如何判断当前帧是否为父帧?

  •  5
  • Tom  · 技术社区  · 15 年前

    到现在为止我一直在试着和

    window.parent != null
    

    但它总是返回false,就像在这个简单的例子中。

    <html>
    <head>
      <script>
        alert(parent == null);
      </script>
    </head>
    
    <body>
      <h1>OH YEAH!</h1>
    </body>
    </html>
    

    有办法吗?我不需要便携,现在我正在寻找IE6解决方案。

    3 回复  |  直到 8 年前
        1
  •  7
  •   Wladimir Palant    13 年前

    我发现这个pdf非常有用: http://seclab.stanford.edu/websec/framebusting/framebust.pdf

    <style>
      html { display :none; }
    </style>
    <script>
    if(self==top){
      document.documentElement.style.display = 'block';
    }else{
    top.location=self.location;
    }
    </script>
    

    你会发现许多其他方法来做这个pdf和每种方法的利弊。 显然,在没有JavaScript的浏览器上,这种解决方案可能会很痛苦;)

        2
  •  7
  •   NickFitz    15 年前
    self === top
    

        3
  •  1
  •   Daniel Egeberg    15 年前

    你可以检查一下 top.frames.length == 0 .