怎么样判断一个页面是否有父窗体??
例如:在a.html中有这么一段:
<iframe name="top" src="b.html" width="800" height="75" ></iframe>
就是说在a.html中打开b.html,我想在b.html中看看他是不是子窗体,也就是看看他有没有父窗体.怎么样判断!!
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看看效果 楼上的我有点疑问,就是parent.document.title,你根据title是否为空来判断是否有些牵强,如果父窗体的title确实是空的怎么办? 直接用parent.window来判断就行 谢谢大虾们给我解决问题!! 但是问题又出来了:就是我单独打开b.html时,也出现有父窗体的提示!! 用parent.window == window 来判断是否是单独打开b.html
ture表示b.html是单独打开的
false就是由其他网页的iframe打开的 谢谢!
页:
[1]
