窗口间的关系及交互(一)

本文详细解释了JavaScript中不同窗口对象间的关系,包括window.parent、window.top等属性的作用及使用场景,并通过实例展示了这些属性如何帮助开发者理解页面层级结构。

1.window.parent      

   当前窗口的上一级窗口,当前窗口可以是在iframe 中或是frameset 的一个frame中

2.window.top   当前窗口的最顶级窗口。

   不管当前窗口是嵌了多少层或是通过iframe 或是frame嵌套,window.top 将返回最外层窗口。

  说明:如果当前窗口不在iframe 或frame 中,window.top 或window.parent 将返回本窗口对象。

3.window.opener

    通过window.opener方式打开当前窗口的窗口。

4.window.窗口名称

   访问当前窗口的子窗口通过window的name 来访问。

 

举个简单的例子现在有a.htm,b.htm,c.htm,d.htm四个文件。

a.htm

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>无标题页</title>
</head>
<body bgcolor="blue">
    AAAAAAAAAAAA
    
<iframe src="b.htm" name="bChild"></iframe>
    
</body>
</html>
 

 

b.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title></title>
</head>
<body bgcolor="green">
    BBBBBBBB

    
<input type="button" value="click" onclick="javascript:Show();" />
</body>
</html>
 
<script type="text/javascript">
    
function Show()
    {
        alert(
"B的parent窗口是:"+ window.parent.location.pathname);
        alert(
"B的top窗口是:"+ window.top.location.pathname);
        
        
var a = window.parent;
        
        alert(
"a的parent窗口是:"+ a.parent.location.pathname);
        alert(
"a的top窗口是:"+ a.top.location.pathname);
        
        
var c = a.parent;
        
        alert(
"c的parent窗口是:"+ c.parent.location.pathname);
        alert(
"c的top窗口是:"+ c.top.location.pathname);
        
        
        
var d = window.top;
        
        alert(
"d.cChild窗口是"+ d.cChild.location.pathname);
        alert(
"a.bChild窗口是"+ a.bChild.location.pathname);
        
        
    }
 
</script>

 

 c.htm 

<html>
    
<body  bgcolor="red">
        CCCCCCCCCC
        
<iframe src="a.htm"></iframe>
    
</body>
</html>
 

d.htm

<html>
    
<frameset rows="50%,*">
        
<frame src="c.htm" name="cChild" />
        
<frame />
     
</frameset>
</html>

 

通过浏览器访问d.htm就可以看出这几个窗口之间的关系。具体不在详述。

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值