window.opener用法和用途實(shí)例介紹
更新時(shí)間:2013年08月19日 16:03:00 作者:
window.opener,是通過window.open打開子窗體的父窗體的引用,下面為大家詳細(xì)介紹下其具體的使用方法,感興趣的朋友可以參考下
window.opener,是通過window.open打開子窗體的父窗體的引用。
比如在父窗體parentForm里面,通過window.open("subForm.html"),那么在subform.html中window.opener就代表parentForm。既然在子窗體中能夠拿到父窗體的引用,那么就可以在子窗體中設(shè)置父窗體的字段值或者調(diào)用js方法。
實(shí)例:添加人員信息時(shí),其中的機(jī)構(gòu)信息通過子窗體完成輸入
父親窗體,用于添加人員信息。
子窗體完成輸入后,機(jī)構(gòu)信息(id、name)自動填充到父窗體的orgId、orgName域
html代碼
<tr>
<tdclass="tdEditLabel">機(jī)構(gòu)</td>
<tdclass="tdEditContent" colspan="3"style="width:400px;text-align:left">
<input type="hidden"name="orgId" id="orgIdId">
<!-- disabled修飾的內(nèi)容 不提交 -->
<input type="text"name="orgName" disabled="disabled"id="orgNameId">
<input type="button"name="selectOrgButton" value="選擇機(jī)構(gòu)"
onclick="openWin('org.do?select=true','selectorg',800,500,1)">
</td>
</tr>
JS代碼
/*
*打開新窗口(通過window.open())
* f:鏈接地址
* n:窗口的名稱
* w:窗口的寬度
* h:窗口的高度
* s:窗口是否有滾動條,1:有滾動條;0:沒有滾動條
*/
functionopenWin(f,n,w,h,s){
sb= s == "1" ? "1" : "0";
l= (screen.width - w)/2;
t= (screen.height - h)/2;
sFeatures= "left="+ l +",top="+ t +",height="+ h+",width="+ w
+",center=1,scrollbars=" + sb +",status=0,directories=0,channelmode=0";
openwin= window.open(f , n , sFeatures );
if(!openwin.opener)
openwin.opener= self;
openwin.focus();
returnopenwin;
}
子窗體,供選擇機(jī)構(gòu)信息。
當(dāng)選擇后(通過單擊radio),機(jī)構(gòu)信息(id、name)將填充到父窗體的orgId、orgName域
html代碼
<!--列表數(shù)據(jù)欄 -->
<c:iftest="${!empty pm.datas}">
<c:forEachitems="${pm.datas }" var="org">
<trbgcolor="#EFF3F7" class="TableBody1"onmouseover="this.bgColor = '#DEE7FF';"onmouseout="this.bgColor='#EFF3F7';">
<td align="center"vAlign="center">
<input type="radio"onclick="selectOrg('${org.id }','${org.name }')">
</td>
<tdalign="center" vAlign="center">${org.id}</td>
<tdalign="center" vAlign="center"><ahref="org.do?parentId=${org.id }&select=true">${org.name}</a></td>
<tdalign="center" vAlign="center">${org.sn }</td>
<tdalign="center" vAlign="center">${org.parent.name}</td>
</tr>
</c:forEach>
</c:if>
JS代碼
functionselectOrg(id,name){
if(window.opener){
window.opener.document.all.orgIdId.value= id;
window.opener.document.all.orgNameId.value= name;
window.close();
}
}
選擇機(jī)構(gòu)信息后的結(jié)果
完成機(jī)構(gòu)信息(id、name)的輸入了,只是id在隱藏域中,看不到而已。
小結(jié)
說到對父窗體的引用,除了window.opener,就是window.parent了。window.opener是用于通過window.open方式打開子窗體,而window.parent是用于通過iframe方式打開子窗體。
比如在父窗體parentForm里面,通過window.open("subForm.html"),那么在subform.html中window.opener就代表parentForm。既然在子窗體中能夠拿到父窗體的引用,那么就可以在子窗體中設(shè)置父窗體的字段值或者調(diào)用js方法。
實(shí)例:添加人員信息時(shí),其中的機(jī)構(gòu)信息通過子窗體完成輸入
父親窗體,用于添加人員信息。
子窗體完成輸入后,機(jī)構(gòu)信息(id、name)自動填充到父窗體的orgId、orgName域

html代碼
復(fù)制代碼 代碼如下:
<tr>
<tdclass="tdEditLabel">機(jī)構(gòu)</td>
<tdclass="tdEditContent" colspan="3"style="width:400px;text-align:left">
<input type="hidden"name="orgId" id="orgIdId">
<!-- disabled修飾的內(nèi)容 不提交 -->
<input type="text"name="orgName" disabled="disabled"id="orgNameId">
<input type="button"name="selectOrgButton" value="選擇機(jī)構(gòu)"
onclick="openWin('org.do?select=true','selectorg',800,500,1)">
</td>
</tr>
JS代碼
復(fù)制代碼 代碼如下:
/*
*打開新窗口(通過window.open())
* f:鏈接地址
* n:窗口的名稱
* w:窗口的寬度
* h:窗口的高度
* s:窗口是否有滾動條,1:有滾動條;0:沒有滾動條
*/
functionopenWin(f,n,w,h,s){
sb= s == "1" ? "1" : "0";
l= (screen.width - w)/2;
t= (screen.height - h)/2;
sFeatures= "left="+ l +",top="+ t +",height="+ h+",width="+ w
+",center=1,scrollbars=" + sb +",status=0,directories=0,channelmode=0";
openwin= window.open(f , n , sFeatures );
if(!openwin.opener)
openwin.opener= self;
openwin.focus();
returnopenwin;
}
子窗體,供選擇機(jī)構(gòu)信息。
當(dāng)選擇后(通過單擊radio),機(jī)構(gòu)信息(id、name)將填充到父窗體的orgId、orgName域

html代碼
復(fù)制代碼 代碼如下:
<!--列表數(shù)據(jù)欄 -->
<c:iftest="${!empty pm.datas}">
<c:forEachitems="${pm.datas }" var="org">
<trbgcolor="#EFF3F7" class="TableBody1"onmouseover="this.bgColor = '#DEE7FF';"onmouseout="this.bgColor='#EFF3F7';">
<td align="center"vAlign="center">
<input type="radio"onclick="selectOrg('${org.id }','${org.name }')">
</td>
<tdalign="center" vAlign="center">${org.id}</td>
<tdalign="center" vAlign="center"><ahref="org.do?parentId=${org.id }&select=true">${org.name}</a></td>
<tdalign="center" vAlign="center">${org.sn }</td>
<tdalign="center" vAlign="center">${org.parent.name}</td>
</tr>
</c:forEach>
</c:if>
JS代碼
復(fù)制代碼 代碼如下:
functionselectOrg(id,name){
if(window.opener){
window.opener.document.all.orgIdId.value= id;
window.opener.document.all.orgNameId.value= name;
window.close();
}
}
選擇機(jī)構(gòu)信息后的結(jié)果

完成機(jī)構(gòu)信息(id、name)的輸入了,只是id在隱藏域中,看不到而已。
小結(jié)
說到對父窗體的引用,除了window.opener,就是window.parent了。window.opener是用于通過window.open方式打開子窗體,而window.parent是用于通過iframe方式打開子窗體。
相關(guān)文章
JavaScript數(shù)據(jù)庫TaffyDB用法實(shí)例分析
這篇文章主要介紹了JavaScript數(shù)據(jù)庫TaffyDB用法,實(shí)例分析了TaffyDB數(shù)據(jù)庫的定義、查詢、更新、刪除等操作的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07原生JavaScript實(shí)現(xiàn)的簡單省市縣三級聯(lián)動功能示例
這篇文章主要介紹了原生JavaScript實(shí)現(xiàn)的簡單省市縣三級聯(lián)動功能,結(jié)合完整實(shí)例形式分析了javascript聯(lián)動菜單的實(shí)現(xiàn)方法,涉及javascript事件響應(yīng)及頁面元素動態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05自定義javascript驗(yàn)證框架示例【附源碼下載】
這篇文章主要介紹了自定義javascript驗(yàn)證框架,結(jié)合實(shí)例形式分析了javascript正則驗(yàn)證相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2019-05-05director.js實(shí)現(xiàn)前端路由使用實(shí)例
這篇文章主要介紹了director.js實(shí)現(xiàn)前端路由使用實(shí)例,director.js是最純粹的路由注冊/解析器,它在不刷新頁面的情況下,利用“#”符號組織不同的URL路徑,需要的朋友可以參考下2015-02-02基于勻速運(yùn)動的實(shí)例講解(側(cè)邊欄,淡入淡出)
下面小編就為大家?guī)硪黄趧蛩龠\(yùn)動的實(shí)例講解(側(cè)邊欄,淡入淡出)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Javascript 拖拽的一些簡單的應(yīng)用(逐行分析代碼,讓你輕松了拖拽的原理)
這篇文章主要介紹了Javascript 拖拽的一些簡單的應(yīng)用(逐行分析代碼,讓你輕松了拖拽的原理),需要的朋友可以參考下2015-01-01js 函數(shù)的執(zhí)行環(huán)境和作用域鏈的深入解析
在js中對象的外在表現(xiàn)形式為函數(shù)。2009-11-11