window.parent与window.opener、window.showModalDialog的区别 opener和showModalDialog刷新父页面的方法
项目中使用案例:
父窗体
<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页面调用父页面对象

- <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刷新父页面的方法的更多相关文章
- showModalDialog后如何刷新父页面
最近一个项目使用到的.在网上查了好久,有的可行,有的就不行.总结一下吧.方案一:父页面:window.showModalDialog('User.jsf?USERCODE='001'&Rnd= ...
- js jquery 关闭弹出页面 并刷新父页面(window.opener)
function Closepage() { if (window.opener && !window.opener.closed) { window.parent.opener.lo ...
- js 刷新windows.open另一个窗口页面或window.open的页面如何刷新(父页面)上层页面
一.js完整代码如下: //js打开新窗口 functionopenWin() {window.open('addInfo.jsp', '_blank','width=300,height=400,t ...
- window.open页面关闭后刷新父页面
如题 function openWin(url,text,winInfo){ var winObj = window.open(url,text,winInfo); var loop = setInt ...
- easyUi弹出window窗口传值与调用父页面的方法,子页面给父页面赋值
<!-- 父页面 --> <!DOCTYPE html PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN" "ht ...
- 打开子页面及刷新父页面 window.open window.opener.reload()
//打开子页面 var url=children_url;window.open(url) //刷新parent页面 var url=parent_urlfunction refresh(url){ ...
- window.open 子窗口关闭刷新父页面
function JsMod(htmlurl,tmpWidth,tmpHeight){ htmlurl=getRandomUrl(htmlurl); var winObj = window.open( ...
- js window.open()打开的页面关闭后刷新父页面
function test(){ var winObj = window.open(URL); var loop = setInterval(function(){ if(winObj.closed) ...
- window.parent.document解决原生js或jQuery 实现父窗口的问题
做WEB前端开发的过程中,经常会有这样的需求,用户点击[编辑]按钮,弹出一个对话框,在里边修改相应的值,然后把修改后的值显示在原页面,最后点击保存. 用window.parent.document.g ...
随机推荐
- eclipse----快速设置主题色
- AP聚类
基于代表点的聚类算法可以说是聚类算法中"最经典的,最流行的,也是最前沿的". "最经典"是因为K均值是最早出现的聚类算法之一; "最流行"是 ...
- android学习之路资料集合
版权声明:本文为 stormzhang 原创文章,可以随意转载,但必须在明确位置注明出处!!! 这篇博客背后的故事 一路走来很不容易,刚好知乎上被人邀请回答如何自学android编程, 就借这个机会在 ...
- 前端开发之旅- 移动端HTML5实现文件上传
一. 在一个客户的webapp项目中需要用到 html5调用手机摄像头,找了很多资料,大都是 js调用api 然后怎样怎样,做了几个demo测试发现根本不行, 后来恍然大悟,用html5自带的 in ...
- 查看/进入mac根目录的方式
1.通过“前往文件夹”快捷键组合 (1)打开finder,点击上部菜单栏“前往”,然后“个人”,直接跳转. (2)快捷键组合:command + shift + G:注意:打开finder后,再快捷键 ...
- log4j最全教程
(转自http://www.codeceo.com/article/log4j-usage.html) 日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方 ...
- Centos6.7安装Cacti教程
Centos6.7安装Cacti教程# link:http://docs.cacti.net/plugins/ blog地址:http://www.cnblogs.com/caoguo 一.基本环境安 ...
- 小白年薪26万,为什么Python岗位薪资越来越高?
人工智能和大数据概念的兴起,带动了Python的快速增长——Python语言逻辑简洁.入门简单.生态丰富,几乎成为几个新兴领域的不二选择.而除了这两个领域,Python还有更多的适用领域:爬虫.web ...
- openstack——cinder服务篇
一.cinder 介绍: 理解 Block Storage 操作系统获得存储空间的方式一般有两种: 通过某种协议(SAS,SCSI,SAN,iSCSI 等)挂接裸硬盘,然后分区.格式化.创建文 ...
- Oracle 【基 本 操 作】
1.日期时间 select SYSDATE from DUAl; select TO_CHAR(SYSDATE, 'YYYY-MM-DD') from DUAL; select TO_CHAR(SYS ...