看一下这段代码,或许可以给你点提示,可以直接运行的:
<!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>父窗口</title>
<script language="javascript" type="text/javascript">
<!--
var win=null
//用于存储子窗口对象的全局变量
function output()
{
window.btn1.disabled=true;//禁用btn1,防止打开多个子窗口
var text=window.text1.value;//将要交互的值
//打开一无url的新窗口
win=window.open('','newwin','menubar=no,width=400,height=100,resizeable=no');
//定义新窗口的document对象,以便对其写入代码
var childdoc=win.document;
childdoc.writeln('<html>');
childdoc.writeln('<head>');
childdoc.writeln('<title>新开子窗口</title>');
childdoc.writeln('<script language="javascript" type="text/javascript">function check_son(str){ alert(str) }</script>')
//子窗口的测试函数
childdoc.writeln('</head>');
childdoc.writeln('<body οnunlοad="window.opener.btn1.disabled=false">')
//关闭子窗口后父窗口的btn1恢复可用
childdoc.writeln('<input type="text" name="text2" value="'+text+'" />');
//接收父窗口传值
childdoc.writeln('<input type="button" name="Btn2" value="传回去" οnclick="window.opener.text1.value=self.text2.value;window.opener.btn1.disabled=false;self.close()" />');
//将值传回到父窗口,父窗口的btn1恢复可用,关闭自身
childdoc.writeln('<input type="button" name="Btn3" value="关闭父窗口" οnclick="window.opener.close()" />')
//关闭父窗口
childdoc.writeln('<input type="button" name="Btn3" value="刷新父窗口" οnclick="window.opener.location.reload()" />')
//刷新父窗口
childdoc.writeln('<input type="button" name="Btn3" value="调用父窗口函数" οnclick="window.opener.check_par(this.value)" />')
//调用父窗口函数
childdoc.writeln('</body>');
childdoc.writeln('</html>');
childdoc.close();
//关闭document对象
win.focus();
//子窗口取得焦点
}
function check_par(str)
{
alert(str);
}
-->
</script>
</head>
<body>
<input type="button" name="btn1" value="打开新窗口" id="btn1" οnclick="output()"/>
<input name="text1" type="text" id="text1" size="10" readonly="true" value="要传递的"/>
<input type="button" name="btn2" value="关闭子窗口" id="btn2" οnclick="if (win!=null) win.close()"/>
<input type="button" name="btn3" value="调用子窗口函数" id="btn3" οnclick="if (win!=null) win.check_son(this.value)"/>
</body>
</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>父窗口</title>
<script language="javascript" type="text/javascript">
<!--
var win=null
//用于存储子窗口对象的全局变量
function output()
{
window.btn1.disabled=true;//禁用btn1,防止打开多个子窗口
var text=window.text1.value;//将要交互的值
//打开一无url的新窗口
win=window.open('','newwin','menubar=no,width=400,height=100,resizeable=no');
//定义新窗口的document对象,以便对其写入代码
var childdoc=win.document;
childdoc.writeln('<html>');
childdoc.writeln('<head>');
childdoc.writeln('<title>新开子窗口</title>');
childdoc.writeln('<script language="javascript" type="text/javascript">function check_son(str){ alert(str) }</script>')
//子窗口的测试函数
childdoc.writeln('</head>');
childdoc.writeln('<body οnunlοad="window.opener.btn1.disabled=false">')
//关闭子窗口后父窗口的btn1恢复可用
childdoc.writeln('<input type="text" name="text2" value="'+text+'" />');
//接收父窗口传值
childdoc.writeln('<input type="button" name="Btn2" value="传回去" οnclick="window.opener.text1.value=self.text2.value;window.opener.btn1.disabled=false;self.close()" />');
//将值传回到父窗口,父窗口的btn1恢复可用,关闭自身
childdoc.writeln('<input type="button" name="Btn3" value="关闭父窗口" οnclick="window.opener.close()" />')
//关闭父窗口
childdoc.writeln('<input type="button" name="Btn3" value="刷新父窗口" οnclick="window.opener.location.reload()" />')
//刷新父窗口
childdoc.writeln('<input type="button" name="Btn3" value="调用父窗口函数" οnclick="window.opener.check_par(this.value)" />')
//调用父窗口函数
childdoc.writeln('</body>');
childdoc.writeln('</html>');
childdoc.close();
//关闭document对象
win.focus();
//子窗口取得焦点
}
function check_par(str)
{
alert(str);
}
-->
</script>
</head>
<body>
<input type="button" name="btn1" value="打开新窗口" id="btn1" οnclick="output()"/>
<input name="text1" type="text" id="text1" size="10" readonly="true" value="要传递的"/>
<input type="button" name="btn2" value="关闭子窗口" id="btn2" οnclick="if (win!=null) win.close()"/>
<input type="button" name="btn3" value="调用子窗口函数" id="btn3" οnclick="if (win!=null) win.check_son(this.value)"/>
</body>
</html>