JavaScript中 window.parent 、window.top、window.self代表的含义

在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法打开当前窗口的那个窗口。

      在项目中,有如下应用场景,原本在右侧iframe中引入对话框js类,对话框能出来但是只能在右侧iframe中移动,不能在整个页面内移动。

     解决如下:

     将对话框js类引入到最外层jsp中,然后在原调用出,使用

new window.top.SGS.UI.Control.CommDialog();
window.top.document.body.appendChild(_dialog.divObj);

此次window.top是最顶级父窗口含义。

下面实例:

top作用是取得最顶层框架对象

A.html

 
  
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>prototype test</title>
</head>
<body>
<iframe src="b.html"></iframe>
<script language="javascript">
var topstr = "my top";
    alert(a.html='+top.location.href);
</script>
 
  
</body>
</html>

B.html

 
  
<!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> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>prototype test</title> 
</head> 
<body> 
<iframe src="c.html"></iframe> 
 
  
<script language="javascript"> 
alert('b.html='+top.location.href+";a.html.topstr="+top.topstr);//top.topstr可取a.html(顶层框架参数)的topstr变量 
</script> 
</body> 
</html>

C.html

 
  
<!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> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>prototype test</title> 
</head> 
<body> 
 
  
<script language="javascript"> 
alert('c.html='+top.location.href+";a.html.topstr="+top.topstr);//top.topstr可取a.html(顶层框架参数)的topstr变量 
</script> 
</body> 
</html>

b.html中的top=parent c.html中的top=parent.parent

 

<摘自:http://wangpj.iteye.com/blog/853395 & http://blog.csdn.net/good_youth/article/details/2079416>

转载于:https://www.cnblogs.com/ChandlerVer5/p/js_window_top.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值