How to tell if current frame is parent?

自闭症网瘾萝莉.ら 提交于 2019-12-10 15:33:36

问题


I'm working in a framed environment, and trying to tell if the frame on which some javascript code executes is the top frame (the one that contains the rest).

Up until now I was trying to check it with

window.parent != null

but it always returns false, like in this simple example.

<html>
<head>
  <script>
    alert(parent == null);
  </script>
</head>

<body>
  <h1>OH YEAH!</h1>
</body>
</html>

Is there a way to do this? I doesn't have to be portable, right now I'm looking for the IE6 solution.


回答1:


I found this pdf to be very useful: http://seclab.stanford.edu/websec/framebusting/framebust.pdf

In short, if this is too long to read, this is what they ultimately propose :

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

You will find many other means to do this in this pdf and each means' pro and cons. Obviously, on browsers without JavaScript, this solution could be painful ;)




回答2:


self === top

should return true if executed in the topmost frameset, false otherwise.




回答3:


You can check if top.frames.length == 0.



来源:https://stackoverflow.com/questions/3135380/how-to-tell-if-current-frame-is-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!