中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
JavaScript实际应用:子窗口和父窗口交互
作者:未知 时间:2005-07-27 23:31 出处:CSDN 责编:chinaitpower
              摘要:JavaScript实际应用:子窗口和父窗口交互

JavaScript实际应用:子窗口和父窗口交互


Author: heiyeluren
Date: 2005-07-07
Home:
http://blog.csdn.net/heiyeshuwu

最近项目开发中需要子窗口和父窗口交互的内容,基本上无非就是把子窗口的信息传递给父窗口,并且关闭自己等等,或者是父窗口把自己的信息传递给子窗口等等。

1。父窗口传递信息给子窗口

看代码实例:
<script language=javascript>

function outPut()
{
 //获取父窗口的文本信息赋值给text
 var text = document.abc.text.value;
 //打开子窗口,并且把操作句柄赋值给win变量,以下所有操作都是针对win对象的
 var win = window.open("","mywin", "menubar=no,width=400,height=100,resizeable=yes");
 //输出基本信息
 win.document.writeln("<title>输出结果</title>");
 win.document.writeln("你的信息是:<p>");
 //输出从父窗口获取的信息
 win.document.writeln(text);
 win.document.close();
 win.focus();
}
</script>

<form name=abc method=post>
<input type=text name=text size=50>
//调用上面的函数
<input type=button value=提交 onClick="outPut()">

</form>

2。子窗口传递参数给父窗口

我们对上面的代码进行改造:

<script language=javascript>

function outPut()
{
 var text = document.abc.text.value;
 var win = window.open("","mywin", "menubar=no,width=400,height=100,resizeable=yes");
 win.document.writeln("<title>输出结果</title>");
 win.document.writeln("你的信息是:<p>");
 win.document.writeln(text);
 win.document.writeln("<input type=text name=child value=子窗口信息>");

 //对子窗口本身操作,使用self对象,对父窗口操作使用opener对象,这是关键
 //把子窗口中表单的值回传给父窗口,取代父窗口表单以前的值,然后关闭子窗口
 win.document.writeln("<input type=button value=关闭自己 onClick='window.opener.abc.text.value=self.child.value;self.close()'>");
 //可以控制关闭父窗口
 win.document.writeln("<input type=button value=关闭父窗口 onClick='window.opener.opener=null;window.opener.close()'>");
 //刷新父窗口
 win.document.writeln("<input type=button value=刷新父窗口 onClick='window.opener.location.reload()'>");

 win.document.close();
 win.focus();
}
</script>

<form name=abc method=post>
<input type=text name=text size=50>
<input type=button value=提交 onClick="outPut()">

</form>

3。不是同页面的子窗口和父窗口交互

假设我们涉及到外部程序,比如php、asp等等,那么我们处理的可能是两个页面,比如,上传功能,我们就是需要打开一个新页面,然后再把新页面中的值传递给父页面。

局部代码实例:

<input type="input" value="" name="input_tag" id = "input_tag" onKeyUp="clearPreTagStyle()" size="40">
<input type="hidden" value="0" name="tagid" id="tagid">
<input type="button" value="标签" onclick="popUpWindow('tag.php?tag='+escape(document.tryst_form.input_tag.value))">

以上是父窗口的部分代码,里面的popUpWindow是封装好的window.open函数,所以理解面面的tag.php是另外一个页面就可以,我们需要把当前表单中的值提交给tag.php页面去处理。


tag.php部分代码:

查询标签结果:
<a href="#" name="tag_1">生活</a><a href="#" onclick="opener.document.tryst_form.input_tag.value = document.tag_1.innerHTML">加入该标签</a>

<a href="#" name="tag_2">生活秀</a><a href="#" onclick="opener.document.tryst_form.input_tag.value = document.tag_2.innerHTML">加入该标签</a>

这个就是我们的子窗口,我们要把tag_1和tag_2返回到子窗口中,虽然他们不是同一个页面。这里有个知识点,就是我们如何获取连接中的值,我们使用innerHTML属性:document.tag_2.innerHTML 这个就是我们获取了tag_2的值“生活秀”,我们也能使用其他方法获取,比如:document.all.tag_2.innerHTML,或者this.innerHTML就是指本身的链接的值。

访问父窗口也是使用opener对象来处理:opener.document.tryst_form.input_tag.value,就能够改变父窗口的值。


基本我目前了解就是如此,以后有东西继续添加。

--------------------------------------------------------------------------------------------------------------
参考文章:

http://www.cnbug.com/Article/{C96F3C33-E894-4982-B5C2-7F87D1C2AEAC}.htm

http://www.blueidea.com/bbs/newsdetail.asp?id=142888
http://www.cnblogs.com/gxh973121/archive/2005/04/05/132664.aspx
http://51js.zahui.net/html/1/15100.htm


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有