j2ee126 发表于 2008-3-21 11:03

怎么样判断一个页面是否有父窗体??

例如:
在a.html中有这么一段:
<iframe name="top" src="b.html" width="800" height="75" ></iframe>
就是说在a.html中打开b.html,我想在b.html中看看他是不是子窗体,也就是看看他有没有父窗体.怎么样判断!!

编程之星 发表于 2008-3-23 00:46

用以下的方法试试看吧
a.html的源代码:
<title>A是父窗体</title>
a.html
<iframe name="top" src="b.html" width="800" height="75" ></iframe>
b.html的源代码:
<html>
<script>
function window.onload()
{
   if(parent.document.title=="") //关键代码.parent是指对父窗体的引用
   {
      alert("b.html没有父窗体");
   }else{
      alert("a.html是b.html的父窗体");
   }
}
</script>
<body>
b.html
</body>
</html>

分别然后运行a.html和b.html看看效果

寂寞的刺猬 发表于 2008-3-23 15:00

楼上的我有点疑问,就是parent.document.title,你根据title是否为空来判断是否有些牵强,如果父窗体的title确实是空的怎么办?

渚薰 发表于 2008-3-24 08:51

直接用parent.window来判断就行

j2ee126 发表于 2008-3-24 13:17

谢谢大虾们给我解决问题!!

j2ee126 发表于 2008-3-24 13:25

但是问题又出来了:就是我单独打开b.html时,也出现有父窗体的提示!!

渚薰 发表于 2008-3-25 08:55

用parent.window == window 来判断是否是单独打开b.html
ture表示b.html是单独打开的
false就是由其他网页的iframe打开的

j2ee126 发表于 2008-3-25 15:55

谢谢!

页: [1]

编程论坛