---
title: "子窗口和父窗口交互"
author: "cui09"
date: "2007-04-01T12:34:46Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-04-01T12:34:46Z"
id: 67078
url: "https://cui.iteye.com/blog/67078"
---

java 代码

01. 最近项目开发中需要子窗口和父窗口交互的内容，基本上无非就是把子窗口的信息传递给父窗口，并且关闭自己等等，或者是父窗口把自己的信息传递给子窗口等等。
02. 1。父窗口传递信息给子窗口
03. 看代码实例：
04. &lt;script language=javascript&gt;
05. function outPut()
06. {
07. //获取父窗口的文本信息赋值给text
08. var text = document.abc.text.value;
09. //打开子窗口，并且把操作句柄赋值给win变量，以下所有操作都是针对win对象的
10. var win = window.open("","mywin", "menubar=no,width=400,height=100,resizeable=yes");
11. //输出基本信息
12. win.document.writeln("&lt;title&gt;输出结果&lt;/title&gt;");
13. win.document.writeln("你的信息是：&lt;p&gt;");
14. //输出从父窗口获取的信息
15. win.document.writeln(text);
16. win.document.close();
17. win.focus();
18. }
19. &lt;/script&gt;
20. &lt;form name=abc method=post&gt;
21. &lt;input type=text name=text size=50&gt;
22. //调用上面的函数
23. &lt;input type=button value=提交 onClick="outPut()"&gt;
24. &lt;/form&gt;
25. 2。子窗口传递参数给父窗口
26. 我们对上面的代码进行改造：
27. &lt;script language=javascript&gt;
28. function outPut()
29. {
30. var text = document.abc.text.value;
31. var win = window.open("","mywin", "menubar=no,width=400,height=100,resizeable=yes");
32. win.document.writeln("&lt;title&gt;输出结果&lt;/title&gt;");
33. win.document.writeln("你的信息是：&lt;p&gt;");
34. win.document.writeln(text);
35. win.document.writeln("&lt;input type=text name=child value="http://www.blog.com.cn/子窗口信息&gt;");
36. //对子窗口本身操作，使用self对象，对父窗口操作使用opener对象，这是关键
37. //把子窗口中表单的值回传给父窗口，取代父窗口表单以前的值，然后关闭子窗口
38. win.document.writeln(""&lt;input type=button value=关闭自己 onClick='window.opener.abc.text.value="http://www.blog.com.cn/self.child.value;self.close()'&gt;");
39. //可以控制关闭父窗口
40. win.document.writeln(""&lt;input type=button value=关闭父窗口 onClick='window.opener.opener=null;window.opener.close()'&gt;");
41. //刷新父窗口
42. win.document.writeln("&lt;input type=button value=刷新父窗口 onClick='window.opener.location.reload()'&gt;");
43. win.document.close();
44. win.focus();
45. }
46. &lt;/script&gt;
47. &lt;form name=abc method=post&gt;
48. &lt;input type=text name=text size=50&gt;
49. &lt;input type=button value=提交 onClick="outPut()"&gt;
50. &lt;/form&gt;
51. 3。不是同页面的子窗口和父窗口交互
52. 假设我们涉及到外部程序，比如php、asp等等，那么我们处理的可能是两个页面，比如，上传功能，我们就是需要打开一个新页面，然后再把新页面中的值传递给父页面。
53. 局部代码实例：
54. &lt;input type="input" value="" name="input\_tag" id = "input\_tag" onKeyUp="clearPreTagStyle()" size="40"&gt;
55. &lt;input type="hidden" value="0" name="tagid" id="tagid"&gt;
56. &lt;input type="button" value="标签" onclick="popUpWindow('tag.php?tag='+escape(document.tryst\_form.input\_tag.value))"&gt;
57. 以上是父窗口的部分代码，里面的popUpWindow是封装好的window.open函数，所以理解面面的tag.php是另外一个页面就可以，我们需要把当前表单中的值提交给tag.php页面去处理。
58. tag.php部分代码：
59. 查询标签结果：
60. &lt;a href="#" name="tag\_1"&gt;生活&lt;/a&gt;&lt;a href="#" onclick="opener.document.tryst\_form.input\_tag.value = document.tag\_1.innerHTML"&gt;加入该标签&lt;/a&gt;
61. &lt;a href="#" name="tag\_2"&gt;生活秀&lt;/a&gt;&lt;a href="#" onclick="opener.document.tryst\_form.input\_tag.value = document.tag\_2.innerHTML"&gt;加入该标签&lt;/a&gt;
62. 这个就是我们的子窗口，我们要把tag\_1和tag\_2返回到子窗口中，虽然他们不是同一个页面。这里有个知识点，就是我们如何获取连接中的值，我们使用innerHTML属性：document.tag\_2.innerHTML 这个就是我们获取了tag\_2的值“生活秀”，我们也能使用其他方法获取，比如：document.all.tag\_2.innerHTML，或者this.innerHTML就是指本身的链接的值。
63. 访问父窗口也是使用opener对象来处理：opener.document.tryst\_form.input\_tag.value，就能够改变父窗口的值。