---
title: "窗口刷新父窗口然后关闭"
author: "bestwinner"
date: "2010-06-17T23:19:44Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2010-06-17T23:44:29Z"
id: 693455
url: "https://bestwinner.iteye.com/blog/693455"
---

<span style="color:green">//第一篇</span>
opener.document.location.reload() 

最佳答案
1    history.go(0) 
2    location.reload() 
3    location=location 
4    location.assign(location) 
5    document.execCommand('Refresh') 
6    window.navigate(location) 
7    location.replace(location) 
8    document.URL=location.href
这几个都可以刷新
比如：父窗口 
<a href="javascript:void(0)" onclick="window.open('child.html','child','width=400,height=300,left=200,top=200');">打开子窗口</a> 

子窗口 
<script language="JavaScript" type="text/javascript"> 
<!-- 
function refreshParent() { 
window.opener.location.href = window.opener.location.href; 
if (window.opener.progressWindow) 
{ 
window.opener.progressWindow.close(); 
} 
window.close(); 
} //--> 
</script> 

<a href="javascript:void(0)" onclick="refreshParent()">刷新父窗口并关闭当前窗口</a> 

<span style="color:green">//////////////////////////////////
第二篇</span>

showModalDialog()子窗口刷新父窗口 
1 子窗口刷新父窗口 

   如果是window.open();问题就好办，直接用window.opener.location.reload();就搞定了

   但是如果是window.showModalDialog(),它里面是没有window.opener的，不能用opener引用父窗口，直接调用用报错。

总结了一下有两种方法可以解决，一是在子窗口中刷新父窗口，二是在子窗口关闭后，父窗口自己刷新。

（1）在子窗口中刷新父窗口 

       父窗口： 因为子窗口不能直接引用父窗口，所以需要父窗口主动传递指针变量，就是window.showModalDialog的第二个参数，参数为“window”,

示例：

var returnValue = window.showModalDialog(url,window,"dialogHeight=350px;dialogWidth=400px;center=yes;status=no;scroll=no;resizable=yes")；

注意：window不要加引号。 

子窗口：执行完操作后调用window.dialogArgument.location.reload();

网上说只调用这一句就可以了，但我试了不行，具体原因没查到，自己猜测要这么用，父窗口必须是iframe. 如果父窗口不是iframe，想要实现这个功能，还可以在body中加入"onUnload=window.dialogArgument.location.reload()" ，这样子窗口关闭时就可以刷新父窗口了。

(2)在父窗口中刷新自己 

这个实现比较简单，在子窗口关闭后，父窗口自己刷新，例如:

var returnValue = window.showModalDialog(url,window,"dialogHeight=350px;dialogWidth=400px;center=yes;status=no;scroll=no;resizable=yes");
        if(returnValue == "true"){
            window.location.reload();
        }

还有一种方法就是在子窗口中调用父窗口的一个方法，该方法自己刷新本页面（父窗口）；

子窗口：window.dialogArgument.reloadPage();window.close;

父窗口: function reloadPage（）{window.location.reload();}

但是我试验后不行，能调用到这个方法，但是就是不刷新，原因不详。

2 showModalDialog参数问题： 

vReturnValue = window.showModalDialog(sURL  );

第三个参数用来描述对话框的外观等信息，我调整了半天都不见效，后来发现原因出现在两个地方：

（1）长度，宽度，都是用 dialogHeight， dialogWidth表示，而不是heigh,width，和window.open()是不同的。

(2)多个参数用";"分割，而不是用","。

为了您的安全，请只打开来源可靠的网址
打开网站    取消
来自: http://hi.baidu.com/59le/blog/item/84ecb6afe08b69c67dd92a58.html