项目中使用案例:

父窗体

<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
<html>
<head><title>父页面</title></head>
<body>
<form name="form1" id="form1">
<input type="text" name="username" id="username"/>
</form>
<iframe src="b.html" width=100%></iframe>
</body>
</html>

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

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

实例源码:

1.a.html

<html>
<head>
<title>主页面</title>
<script>
/** 为测试IFrame子窗口调用父窗口的全局变量而添加的测试变量 */
var parentVairous = "为测试IFrame子窗口调用父窗口的全局变量而添加的测试变量";
function parentInvokeIFrame()
{
var iframeTest = document.frames["iframeTest"]; //使用document.getElementById("iframeTest");同样可以
alert(iframeTest.document.body.innerHTML);
alert(iframeTest.iFrameVair);
}
</script>
</head>
<body>
<form name="form1" id="form1">
<input type="text" name="username" id="username"/>
<input type = "button" value = "父窗口调用IFrame子窗口中的内容" onclick = 'parentInvokeIFrame()'/>
</form>
<iframe src="b.html" width = '100%' id = 'iframeTest'></iframe>
</body>
</html>

.b.html

<html>
<head>
<title></title>
<script type="text/javascript">
/** 为测试父窗体调用IFrame子窗体的全局函数而添加的子窗口全局函数 */
var iFrameVair = "测试父窗体调用IFrame子窗体的全局函数"; function UpdateParent()
{
var _parentWin = window.parent ;
_parentWin.form1.username.value = "xxxx" ;
} function childInvokeParent()
{
var parentVairous = window.parent.window.parentVairous;
alert(parentVairous);
}
</script>
</head>
<body>
<form name="form1" id="form1">
<p> </p>
<p align="center">
<input type = "button"
name = "button"
id = "button"
value = "更新主页面的UserName内容"
onclick = "UpdateParent()">
<input type = "button"
name = "button2"
id = "button2"
value = "测试IFrame子窗口调用父窗口的全局变量"
onclick = "childInvokeParent();"/>
</p>
<p> </p>
</form>
</body>
</html>

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

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

源码:
2.a.html

<html>
<head>
<title>主页面</title>
<script type="text/javascript">
/** 为测试IFrame子窗口调用父窗口的全局变量而添加的测试变量 */
var parentVairous = "为测试IFrame子窗口调用父窗口的全局变量而添加的测试变量"; /**
* 因为不同于IFrame(IFrame有id,window.open()与IFrame的父子窗口的模式不同),
* 所以当是通过window.open()方法打开一个新窗口使, 必须有一个新窗口的对象
* 当然必须先让子窗口弹出来, 才能调用子窗口中的变量, 否则抛出异常
*/
var OpenWindow; function openSubWin()
{
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');
}
function parentInvokeChild()
{
if(OpenWindow)//当然必须先让子窗口弹出来, 才能调用子窗口中的变量, 否则抛出异常
{
alert(OpenWindow.iFrameVair);
}
}
</script>
</head>
<body>
<form name="form1" id="form1">
<input type="text" name="username" id="username"/>
<input type="button" value="弹出子页面" onclick = "openSubWin()">
<input type="button" value="测试调用弹出窗口中的全局变量" onclick = "parentInvokeChild()">
</form>
</body>
</html>

2.b.html

<html>
<head>
<title>子页面</title>
<script type="text/javascript">
/** 为测试父窗体调用IFrame子窗体的全局函数而添加的子窗口全局函数 */
var iFrameVair = "测试父窗体调用IFrame子窗体的全局函数";
function UpdateParent()
{
var _parentWin = window.opener;
_parentWin.form1.username.value = "xxxx" ;
}
function childInvokeParent()
{
var parentVairous = window.opener.window.parentVairous;
alert(parentVairous);
}
</script>
</head>
<body>
<form name="form1" id="form1">
<p> </p>
<p align="center">
<input type="button"
onclick = "UpdateParent();"
name="button"
id="button"
value="更新主页面的UserName内容">
<input type = "button"
name = "button2"
id = "button2"
value = "测试IFrame子窗口调用父窗口的全局变量"
onclick = "childInvokeParent();"/>
</p>
<p> </p>
</form>
</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错误。

<html>
<body>
<form. name=form1>
<input type=text name=inpu >
<input type=button >
</form>
</body>
</html>

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

<html>
<body>
<form. name=form1>
<input type=text name=inpu > <a href=# >添加</a>
</form>
</body>
</html>

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

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

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

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

child.html :

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= " ">
<META NAME= "Keywords " CONTENT= " ">
<META NAME= "Description " CONTENT= " "> <SCRIPT LANGUAGE=javascript FOR=window EVENT=onload>
<!--
var s = new Object();//这里是关键若用户为单击按钮,已其它方式关闭按钮,则把type= " " 传递回去。以免出现问题。且刷新父页面。
s.type= " ";
window.returnValue = s;
//-->
</SCRIPT> </HEAD> <BODY>
<input type = "button " value= "返回不刷新 " onclick= "doSearch() ">
</BODY>
</HTML> <script language=javascript>
<!--
var k=window.dialogArguments;
//使用传递过来的 "aaa ";
//..........
function doSearch()
{
var s = new Object();
s.type= "OK ";//设置返回值。//这里返回不刷新父页面。
window.returnValue=s;
window.close();
}
//-->
</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. 慕课网3-13编程练习:采用flex弹性布局制作页面主导航

    小伙伴们,伸缩容器的属性我们已经学完了,接下来使用我们所学的伸缩容器属性完成下面的效果图. 要求: 1.logo.导航项.登录注册按钮这三项在水平和垂直方向上都对齐,而且他们之间的距离也相等. 2.导 ...

  2. C语言过时了?为什么还要推荐每一位程序员都来学一下C语言?

    互联网蓬勃发展的时代,有一类人做出了巨大的贡献,这一群人被大家称之为程序员,怎样才能成为一名优秀的程序员呢,为什么每一个程序员都需要学习C语言呢? 就让我来跟大家分享分享:   在学习C/C++或者想 ...

  3. scrapy 简单操作

    1.创建一个简单的scrapy项目 scrapy startproject search(项目名称)按照提示cd searchscrapy genspider serachname search.co ...

  4. [转]Linux命令wc的详细用法

    转自:http://blog.hehehehehe.cn/a/17301.htm wc命令用来打印文件的文本行数.单词数.字节数等(print the number of newlines, word ...

  5. Unity相机平滑跟随

    简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...

  6. 生成错误:对路径".dll"的访问被拒绝

    第一步:检查dll所在的目录的访问权限,右键文件夹>属性>安全>设置添加EveryOne用户并将完全控制的权限赋给它. 如果问题还没有解决,请不要一遍遍的重启,看第二步: 第二步:右 ...

  7. C#——反射动态创建类的实例

    “反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间. 若要反射当前项目中的类(即当前项目已经引用它了),可以使用下面的写法. ...

  8. java攻城师之路--复习java web之servlet

    需要掌握的知识点:1.Servlet程序编写 ----- 生命周期2.ServletAPI Request Response 3.Cookie 和 Session Servlet 用来 动态web资源 ...

  9. ICMP,ARP协议

    ICMP ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息.控制 ...

  10. querySelector

     this.el.nativeElement.querySelector import {Component, ElementRef, OnInit} from '@angular/core';