项目中使用案例:

父窗体

<s:form namespace="/forexagent" id="listSearchForm" name="theForm"
    theme="simple" action="listAgentParametersChangeRecord?funcode=011002005">

......

<s:textfield cssClass="px154 pd-5" name="searchCriteriaAgentNo" id="searchCriteria.agentNo"/>

......

</s:form>

function getSelectAgent(){
     var ids = $("#searchCriteria\\.agentNo").val();
     var page = "selectAgentNoList?id="+ids;
     OpenWindow=window.open(page, 'newwindow'+new Date(), 'height=600, width=800, top=300,left=300, toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, status=no');
    
}

子窗体:

var setting = {
        data: {
            simpleData: {
                enable:true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: null
            }
        },
        check:{//ztree用于树形复选框中必选的配置
            enable: true,
            chkboxType : {
                 "Y" : "ps",
                 "N" : "ps"}
             }

}

function initMyZtree(){
        $.fn.zTree.init($("#tree"), setting, zNodes);
        var zTree = $.fn.zTree.getZTreeObj("tree");
        var selectNode = '<%=request.getParameter("id")%>';
        alert(selectNode);
        var nodes=selectNode.split(",");      
        for (i=0;i<nodes.length ;i++ )   
        {   
            alert(nodes[i]);
            if(nodes[i] && nodes[i]!=""){
                zTree.selectNode(zTree.getNodeByParam("id", nodes[i]),true); //选中节点(节点变色)
                   zTree.checkNode(zTree.getNodeByParam("id", nodes[i]), true, false);//选中节点(复选框中出现勾号)
            }
        }   
    }

<input type="button" value="确定" style="margin-right:20px" onclick="confirmSelectAgent()" class="blue" />

function confirmSelectAgent(){  
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes = treeObj.getCheckedNodes(true);
        var sb='';
        for(var i=0;i<nodes.length;i++){
           sb+=nodes[i].name+",";//父节点
        }
        if(sb.length>0) sb=sb.substring(0,sb.length-1);
        var _parentWin = window.opener;  
        _parentWin.listSearchForm.searchCriteriaAgentNo.value = sb ;  //向父窗体中输入框中赋值
        window.close()
    }

转自:http://www.cnblogs.com/jinianjun/archive/2012/07/29/2614229.html

1:   window.parent 是iframe页面调用父页面对象

a.html
  1. <html>
  2. <head><title>父页面</title></head>
  3. <body>
  4. <form name="form1" id="form1">
  5. <input type="text" name="username" id="username"/>
  6. </form>
  7. <iframe src="b.html" width=100%></iframe>
  8. </body>
  9. </html>

如果我们需要在b.htm中要对a.htm中的username文本框赋值,就如很多上传功能,上传功能页在Ifrmae中,上传成功后把上传后的路径放入父页面的文本框中
我们应该在b.html中写

  1. <script type="text/javascript">
  2. var _parentWin = window.parent ;
  3. _parentWin.form1.username.value = "xxxx" ;
  4. </script>

实例源码:

1.a.html

  1. <html>
  2. <head>
  3. <title>主页面</title>
  4. <script>
  5. /** 为测试IFrame子窗口调用父窗口的全局变量而添加的测试变量 */
  6. var parentVairous = "为测试IFrame子窗口调用父窗口的全局变量而添加的测试变量";
  7. function parentInvokeIFrame()
  8. {
  9. var iframeTest = document.frames["iframeTest"]; //使用document.getElementById("iframeTest");同样可以
  10. alert(iframeTest.document.body.innerHTML);
  11. alert(iframeTest.iFrameVair);
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. <form name="form1" id="form1">
  17. <input type="text" name="username" id="username"/>
  18. <input type = "button" value = "父窗口调用IFrame子窗口中的内容" onclick = 'parentInvokeIFrame()'/>
  19. </form>
  20. <iframe src="b.html" width = '100%' id = 'iframeTest'></iframe>
  21. </body>
  22. </html>

.b.html

  1. <html>
  2. <head>
  3. <title></title>
  4. <script type="text/javascript">
  5. /** 为测试父窗体调用IFrame子窗体的全局函数而添加的子窗口全局函数 */
  6. var iFrameVair = "测试父窗体调用IFrame子窗体的全局函数";
  7.  
  8. function UpdateParent()
  9. {
  10. var _parentWin = window.parent ;
  11. _parentWin.form1.username.value = "xxxx" ;
  12. }
  13.  
  14. function childInvokeParent()
  15. {
  16. var parentVairous = window.parent.window.parentVairous;
  17. alert(parentVairous);
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <form name="form1" id="form1">
  23. <p> </p>
  24. <p align="center">
  25. <input type = "button"
  26. name = "button"
  27. id = "button"
  28. value = "更新主页面的UserName内容"
  29. onclick = "UpdateParent()">
  30. <input type = "button"
  31. name = "button2"
  32. id = "button2"
  33. value = "测试IFrame子窗口调用父窗口的全局变量"
  34. onclick = "childInvokeParent();"/>
  35. </p>
  36. <p> </p>
  37. </form>
  38. </body>
  39. </html>

ps:不能跨域获取,例如iframe的src是'http://www.xxx.ccc/'就不可以

2:   window.opener 是window.open 打开的子页面调用父页面对象

源码:
2.a.html

  1. <html>
  2. <head>
  3. <title>主页面</title>
  4. <script type="text/javascript">
  5. /** 为测试IFrame子窗口调用父窗口的全局变量而添加的测试变量 */
  6. var parentVairous = "为测试IFrame子窗口调用父窗口的全局变量而添加的测试变量";
  7.  
  8. /**
  9. * 因为不同于IFrame(IFrame有id,window.open()与IFrame的父子窗口的模式不同),
  10. * 所以当是通过window.open()方法打开一个新窗口使, 必须有一个新窗口的对象
  11. * 当然必须先让子窗口弹出来, 才能调用子窗口中的变量, 否则抛出异常
  12. */
  13. var OpenWindow;
  14.  
  15. function openSubWin()
  16. {
  17. OpenWindow = window.open('b.html', 'newwindow', 'height=1024, width=1300, top=0, left=0, toolbar=no, menubar=yes, scrollbars=yes,resizable=yes,location=no, status=no');
  18. }
  19. function parentInvokeChild()
  20. {
  21. if(OpenWindow)//当然必须先让子窗口弹出来, 才能调用子窗口中的变量, 否则抛出异常
  22. {
  23. alert(OpenWindow.iFrameVair);
  24. }
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <form name="form1" id="form1">
  30. <input type="text" name="username" id="username"/>
  31. <input type="button" value="弹出子页面" onclick = "openSubWin()">
  32. <input type="button" value="测试调用弹出窗口中的全局变量" onclick = "parentInvokeChild()">
  33. </form>
  34. </body>
  35. </html>

2.b.html

  1. <html>
  2. <head>
  3. <title>子页面</title>
  4. <script type="text/javascript">
  5. /** 为测试父窗体调用IFrame子窗体的全局函数而添加的子窗口全局函数 */
  6. var iFrameVair = "测试父窗体调用IFrame子窗体的全局函数";
  7. function UpdateParent()
  8. {
  9. var _parentWin = window.opener;
  10. _parentWin.form1.username.value = "xxxx" ;
  11. }
  12. function childInvokeParent()
  13. {
  14. var parentVairous = window.opener.window.parentVairous;
  15. alert(parentVairous);
  16. }
  17. </script>
  18. </head>
  19. <body>
  20. <form name="form1" id="form1">
  21. <p> </p>
  22. <p align="center">
  23. <input type="button"
  24. onclick = "UpdateParent();"
  25. name="button"
  26. id="button"
  27. value="更新主页面的UserName内容">
  28. <input type = "button"
  29. name = "button2"
  30. id = "button2"
  31. value = "测试IFrame子窗口调用父窗口的全局变量"
  32. onclick = "childInvokeParent();"/>
  33. </p>
  34. <p> </p>
  35. </form>
  36. </body>

经过网友的提醒,确实需要注意的是,模态窗口的子窗口是没有办法修改父窗口页面中的任何内容的。

如修改:OpenWindow = window.open('b.html', 'newwindow', 'height=1024,
width=1300, top=0, left=0, toolbar=no, menubar=yes,
scrollbars=yes,resizable=yes,location=no, status=no');
为:OpenWindow = window.showModalDialog("b.html",'newwindow',"dialogHeight:100px,center:yes,resizable:no,status:no");
在子窗口中当希望修改父窗口中的内容时,会弹出“某某”为空或不是对象的错误,而这里的“某某”就是你想修改的父窗口中的内容

需要注意的是:模态窗口调用父窗口也是window.parent来处理的。但是模态和window.open有个功能上
的区别就是,模态的窗口不能刷新父页面(刷新整个页面,比如用window.location或form.submit等的处理),而使用
window.open就可以进行刷新父窗口的操作

说说javascript中几个内置对象的含义和使用方法。opener,self,parent

opener:对打开当前窗口的window对象的引用,如果当前窗口已被用户打开,则opener的值为null.

self:自引用属性,是对当前window对象的应用,与window属性同义.

self代表自身窗口,opener代表打开自身的那个窗口,比如窗口A打开窗口B.如果靠window.open方法,则对于窗口B,self代表B自己,而opener代表窗口A.

opener即谁打开我的,比如A页面利用window.open弹出了B页面窗口,那么A页面所在窗口就是B页面的
opener,在B页面通过opener对象可以访问A页面。
parent表示父窗口,比如一个A页面利用iframe或frame调用B页面,那么A页面所在窗口就是B页面的parent。

在JS中,window.opener只是对弹出窗口的母窗口的一个引用。比如:
a.html中,通过点击按钮等方式window.open出一个新的窗口b.html。那么在b.html中,就可以通过

window.opener(省略写为opener)来引用a.html,包括a.html的document等对象,操作a.html的内容。
假如这个引用失败,那么将返回null。所以在调用opener的对象前,要先判断对象是否为null,否则会出现“对象为空或者不存在”的JS错误。

  1. <html>
  2. <body>
  3. <form. name=form1>
  4. <input type=text name=inpu >
  5. <input type=button >
  6. </form>
  7. </body>
  8. </html>

——————————–
back2opener.html
——————————–

  1. <html>
  2. <body>
  3. <form. name=form1>
  4. <input type=text name=inpu >
  5.  
  6. <a href=# >添加</a>
  7. </form>
  8. </body>
  9. </html>

window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了
b.htm,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id为“name”的textbox中,就可以

写为:
window.opener.document.getElementById(“name”).value = “输入的内容”;

下面是opener和showModalDialog刷新父页面的方法:

  1. opener:关闭自己window.close();
  2. 刷新父页面opener.location.reload();
  3. 应该先刷新,再关闭.
  4.  
  5. showModalDialog 的时候:
  6.  
  7. 在子窗口中输出javascript代码windows.returnValue= '1 ',在父窗口中if(showModalDialog(...)== '1 ')   refresh(); 
    refresh()为你自己写的JAVASCRIPT函数,用于刷新页面。
  8.  
  9. 也可以解释如下:
    1.在打开窗口得时候把父窗体做为参数传递给新窗口 
      window.showModalDialog(url,window,style); 
    2.在新打开得窗口得unload得事件中写如下代码: 
    var   args   =   window.dialogArguments; 
    args.location.reload(); 
  10.  
  11. 实例:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
  2. <HTML>
  3. <HEAD>
  4. <TITLE> New Document </TITLE>
  5. <META NAME= "Generator " CONTENT= "EditPlus ">
  6. <META NAME= "Author " CONTENT= " ">
  7. <META NAME= "Keywords " CONTENT= " ">
  8. <META NAME= "Description " CONTENT= " ">
  9. <script language= "javascript ">
  10. <!--
  11. function doSearch(){
  12. var s = new Object();
  13. s.name = "aaa ";
  14. var k = window.showModalDialog( "child.html ",s, "dialogWidth:235px;status:no;dialogHeight:185px ");
  15. if(k.type== " ")//传递回的type为空的时候才刷新页面。
  16. {
  17. alert( "刷新 ");
  18. location.reload();
  19. }
  20. }
  21. //-->
  22. </script>
  23. </HEAD>
  24.  
  25. <BODY>
  26. <input type = "button " value= "openChild " onclick= "doSearch() ">
  27. </BODY>
  28. </HTML>

child.html :

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
  2. <HTML>
  3. <HEAD>
  4. <TITLE> New Document </TITLE>
  5. <META NAME= "Generator " CONTENT= "EditPlus ">
  6. <META NAME= "Author " CONTENT= " ">
  7. <META NAME= "Keywords " CONTENT= " ">
  8. <META NAME= "Description " CONTENT= " ">
  9.  
  10. <SCRIPT LANGUAGE=javascript FOR=window EVENT=onload>
  11. <!--
  12. var s = new Object();//这里是关键若用户为单击按钮,已其它方式关闭按钮,则把type= " " 传递回去。以免出现问题。且刷新父页面。
  13. s.type= " ";
  14. window.returnValue = s;
  15. //-->
  16. </SCRIPT>
  17.  
  18. </HEAD>
  19.  
  20. <BODY>
  21. <input type = "button " value= "返回不刷新 " onclick= "doSearch() ">
  22. </BODY>
  23. </HTML>
  24.  
  25. <script language=javascript>
  26. <!--
  27. var k=window.dialogArguments;
  28. //使用传递过来的 "aaa ";
  29. //..........
  30. function doSearch()
  31. {
  32. var s = new Object();
  33. s.type= "OK ";//设置返回值。//这里返回不刷新父页面。
  34. window.returnValue=s;
  35. window.close();
  36. }
  37. //-->
  38. </script>

window.parent与window.opener、window.showModalDialog的区别 opener和showModalDialog刷新父页面的方法的更多相关文章

  1. showModalDialog后如何刷新父页面

    最近一个项目使用到的.在网上查了好久,有的可行,有的就不行.总结一下吧.方案一:父页面:window.showModalDialog('User.jsf?USERCODE='001'&Rnd= ...

  2. js jquery 关闭弹出页面 并刷新父页面(window.opener)

    function Closepage() { if (window.opener && !window.opener.closed) { window.parent.opener.lo ...

  3. js 刷新windows.open另一个窗口页面或window.open的页面如何刷新(父页面)上层页面

    一.js完整代码如下: //js打开新窗口 functionopenWin() {window.open('addInfo.jsp', '_blank','width=300,height=400,t ...

  4. window.open页面关闭后刷新父页面

    如题 function openWin(url,text,winInfo){ var winObj = window.open(url,text,winInfo); var loop = setInt ...

  5. easyUi弹出window窗口传值与调用父页面的方法,子页面给父页面赋值

    <!-- 父页面 --> <!DOCTYPE html PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN" "ht ...

  6. 打开子页面及刷新父页面 window.open window.opener.reload()

    //打开子页面 var url=children_url;window.open(url) //刷新parent页面 var url=parent_urlfunction refresh(url){  ...

  7. window.open 子窗口关闭刷新父页面

    function JsMod(htmlurl,tmpWidth,tmpHeight){ htmlurl=getRandomUrl(htmlurl); var winObj = window.open( ...

  8. js window.open()打开的页面关闭后刷新父页面

    function test(){ var winObj = window.open(URL); var loop = setInterval(function(){ if(winObj.closed) ...

  9. window.parent.document解决原生js或jQuery 实现父窗口的问题

    做WEB前端开发的过程中,经常会有这样的需求,用户点击[编辑]按钮,弹出一个对话框,在里边修改相应的值,然后把修改后的值显示在原页面,最后点击保存. 用window.parent.document.g ...

随机推荐

  1. eclipse----快速设置主题色

  2. AP聚类

    基于代表点的聚类算法可以说是聚类算法中"最经典的,最流行的,也是最前沿的". "最经典"是因为K均值是最早出现的聚类算法之一; "最流行"是 ...

  3. android学习之路资料集合

    版权声明:本文为 stormzhang 原创文章,可以随意转载,但必须在明确位置注明出处!!! 这篇博客背后的故事 一路走来很不容易,刚好知乎上被人邀请回答如何自学android编程, 就借这个机会在 ...

  4. 前端开发之旅- 移动端HTML5实现文件上传

    一. 在一个客户的webapp项目中需要用到 html5调用手机摄像头,找了很多资料,大都是 js调用api  然后怎样怎样,做了几个demo测试发现根本不行, 后来恍然大悟,用html5自带的 in ...

  5. 查看/进入mac根目录的方式

    1.通过“前往文件夹”快捷键组合 (1)打开finder,点击上部菜单栏“前往”,然后“个人”,直接跳转. (2)快捷键组合:command + shift + G:注意:打开finder后,再快捷键 ...

  6. log4j最全教程

    (转自http://www.codeceo.com/article/log4j-usage.html) 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方 ...

  7. Centos6.7安装Cacti教程

    Centos6.7安装Cacti教程# link:http://docs.cacti.net/plugins/ blog地址:http://www.cnblogs.com/caoguo 一.基本环境安 ...

  8. 小白年薪26万,为什么Python岗位薪资越来越高?

    人工智能和大数据概念的兴起,带动了Python的快速增长——Python语言逻辑简洁.入门简单.生态丰富,几乎成为几个新兴领域的不二选择.而除了这两个领域,Python还有更多的适用领域:爬虫.web ...

  9. openstack——cinder服务篇

    一.cinder 介绍:     理解 Block Storage 操作系统获得存储空间的方式一般有两种: 通过某种协议(SAS,SCSI,SAN,iSCSI 等)挂接裸硬盘,然后分区.格式化.创建文 ...

  10. Oracle 【基 本 操 作】

    1.日期时间 select SYSDATE from DUAl; select TO_CHAR(SYSDATE, 'YYYY-MM-DD') from DUAL; select TO_CHAR(SYS ...