怎么关闭子窗口并且刷新父窗口

如何关闭子窗口并且刷新父窗口
      window.opener.location.reload();这个方法在强迫父窗口的时候,在有些IE浏览器(比如安全设置高)的情况下,会弹出一个确认对话框,提示是不是要重新再刷新一次页面,这可是比较郁闷的事情哦,我后来把这个方法替换成了window.opener.location.href=window.opener.location.href;
就不会出现那样的问题了。

       window.opener其实是指本窗口的父窗口,比如,one.jsp 通过popupwindow打开了two.jsp,哪么在two.jsp里面的window.opener就是指one.jsp,所以在two.jsp里面完全可以用window.opener调用任何一个one.jsp里面的方法,实现one.jsp和two.jsp的交互。
       注意:window.opener.location.href只是一个链接,如果想实现父窗口的提交就要调用window.opener.action="" 和window.opener.submit();方法,但是不幸的是这段代码在firefox下不能运行,解决的办法为在父窗口中写一个提交的function在子窗口中通过window.opener.functionname()调用。
       通常在使用window.opener的时候要去判断父窗口的状态,如果父窗口被关闭或者更新,就会出错,解决办法是加上如下的验证if(window.opener && !window.opener.closed)
有时我们需要在新打开的窗口里面编辑信息,等编辑完了,需要将当前窗口关闭并且刷新父窗口,以使修改生效,本文就是介绍用 javascript 来实现"更新记录后关闭子窗口并刷新父窗口".


1. <script language="JavaScript" type="text/javascript">
2. <!--
3. function refreshParent()
4. {
5.     window.opener.location.href = window.opener.location.href;
6.     if (window.opener.progressWindow)
7.     {
8.         window.opener.progressWindow.close();
9.     }
10.     window.close();
11. }
12. //-->
13. </script>
14.
15. <a href="javascript:void(0)" onclick="refreshParent()">刷新父窗口并关闭当前窗口</a>
16.


1:   window.parent 是iframe页面调用父页面对象

举例:
a.html
17. 程序代码
18. <html>
<head><title>父页面</title></head>
<body>
<form name="form1" id="form1">

<input type="text" name="username" id="username"/>

</form>
<iframe src="b.html" width=100%></iframe>
</body>
</html>
19.

如果我们需要在b.htm中要对a.htm中的username文本框赋值,就如很多上传功能,上传功能页在Ifrmae中,上传成功后把上传后的路径放入父页面的文本框中

我们应该在b.html中写
20. 程序代码
21. <script type="text/javascript">
var _parentWin = window.parent ;
_parentWin.form1.username.value = "xxxx" ;
</script>
22.

实例地址:  http://www.cnspry.cn/blog/attachments/window.parent实例/a.html

2:   window.opener 是window.open 打开的子页面调用父页面对象

a.html
23. 程序代码
24.
<script type="text/javascript">
function openSubWin()
{
var _width = 300 ;
var _height = 200 ;
var _left = (screen.width - _width) / 2 ;
var _top = (screen.height - _height) / 2 ;
window.open("b.html",null,
"height=" + _height + ",width=" + _width + ",status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=" + _left + ",top=" + _top);
}
</script>
<input type="text" name="username" id="username"/>
<input type="button" value="弹出子页面" onClick="openSubWin();">
25.

b.html
26. 程序代码
27.
<script type="text/javascript">
function UpdateParent()
{
var _parentWin = window.opener ;
_parentWin.form1.username.value = "xxxx" ;
}
</script>
<input type="button" name="button" id="button" value="更新主页面的UserName内容" onClick="UpdateParent();">