---
title: "父子窗口方法调用"
author: "guoce"
date: "2010-11-18T15:55:15Z"
tags: []
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2010-11-18T15:55:15Z"
id: 815180
url: "https://guoce.iteye.com/blog/815180"
---

Javascript刷新页面的几种方法：
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 
框架刷新。
window.parent.frames.location.reload();
Page.RegisterStartupScript("","<script language'=javascript'>window.parent.frames.location.reload();
全页面跳转
        Response.Write("<script   language=javascript>parent.location.href='login.aspx';</script>"); 
onclick="window.open('index.aspx','mainFrame');



iframe父子窗口间js方法调用
父窗口调用iframe子窗口方法

<iframe name="myFrame" src="child.html"></iframe> 
myFrame.window.functionName();


iframe子窗口调用父窗口方法

parent.functionName();



子窗口与父窗口的调用  

注意：要用open打开才行，如果用openModal打开则会访问不到opener.
1、从子窗口中往父窗口传值
如父页面有一个控制的id是choosedProvider，那么这样可以把值传给父页面：
opener.document.getElementById("choosedProvider”]).value=”Tom”;
2、从父窗口传值给子窗口，如子窗口中有id为T的控件，此时可以用如下传值：
<script>
var newWindow;
function openWindow(url){
newWindow = window.open(“new.htm”, "", "width=400,height=300 ");
}
function changeChild(){
newWindow.document.getElementById("T").value="我变了";
}
</script>
       先抓住其句柄，然后通过句柄操作，其实比较上面的子窗口往父窗口中传值，就是操作的对象改变了一下。
3、子窗口关闭并刷新父口，这里有两种方式：
a)         子窗口关闭时关闭父窗口：
opener.location.reload();
window.close();
b)        父窗口去检测，子窗口是否已经关闭了，在设定的一段时间之后自动刷新：
<script>
var newWindow;
var timer;
function openWindow(url) {
       newWindow = window.open(url, "", "width=400,height=300,resizable=yes");
       timer = setInterval("updateAfterClose()", 1000);
}
 
function updateAfterClose() {
       //父窗口去检测子窗口是否关闭，然后通过自我刷新，而不是子窗口去刷新父窗口
       if(newWindow.closed == true) {
       clearInterval(timer);
       self.location.reload(); // 主窗口刷新
       return;
       }
}
</script>
4、父窗口关闭的时候关闭子窗口。这个就是用方法去检测父窗口的onunload事件：
<script>
var newWindow;
function openWindow(url) {
newWindow = window.open(url, "", "width=400,height=300,resizable=yes"); 
}
function closeChild()
{
newWindow.close();
}
</script>
在body加上onunload事件：
<body onunload="closeChild()">
5、子窗口中关闭父窗口，这个功能可能相对要少用一点：
<script>
function closeParent()
{
opener.close();
}
</script>
<input type=button value="关闭父窗口" onclick=closeParent()>