a.html:
<form name="form1" method="post" action=""><a href="javascript:void(null)" class="add" onClick="open('b.html','','resizable=1,scrollbars=1,status=no,toolbar=no,menu=no,width=500,height=400,left=150,top=50')">增加</a><input type="text" name="text1"></form>b.html:
<script language="javascript" type="text/javascript">function returnValue(){window.opener.document.all.text1.value=document.getElementById("returnText").value;window.close();}</script> <input type="button" name="Submit" value="提交" onclick="returnValue();"> <input name="returnText" type="text" id="returnText"></p>
補(bǔ)充:window.opener 的用法
window.opener 的用法在一般的用法中,只是用來解決關(guān)閉窗口時(shí)不提示彈出窗口, 而對(duì)它更深層的了解一般比較少。其 實(shí) window.opener是指調(diào)用window.open方法的窗口。
在工作中主要是用來解決部分提交的。這種跨頁操作對(duì)工作是非常有幫助的。
如果你在主窗口打開了一個(gè)頁面,并且希望主窗口刷新就用這個(gè),打開頁面的window.opener就相當(dāng)于
主窗口的window。
主窗口的刷新你可以用
window.opener.location.reload();
如果你用虛擬的目錄:如struts的*.do會(huì)提示你重試
你可以改成這樣 window.opener.yourformname.submit()
就好了
2〉
在應(yīng)用中有這樣一個(gè)情況,
在A窗口中打開B窗口,在B窗口中操作完以后關(guān)閉B窗口,同時(shí)自動(dòng)刷新A窗口
function closeWin(){hasClosed = true;window.opener.location="javascript:reloadPage();";window.close();}function window.onbeforeunload(){if(!hasClosed){window.opener.location="javascript:reloadPage();";}}</script>上面的代碼在關(guān)閉B窗口的時(shí)候會(huì)提示錯(cuò)誤,說缺少Object,正確的代碼如下:
function closeWin(){hasClosed = true;window.opener.location="javascript:reloadPage();";window.opener=null;window.close();}function window.onbeforeunload(){if(!hasClosed){//如果已經(jīng)執(zhí)行了closeWin方法,則不執(zhí)行本方法window.opener.location="javascript:reloadPage();";}}</script>reloadPage方法如下:function reloadPage() {history.go(0);document.execCommand("refresh")document.location = document.location;document.location.reload();}PS:由于需要支持正常關(guān)閉和強(qiáng)制關(guān)閉窗口時(shí)能捕捉到事件,用了全局變量hasClosed
==============================================
補(bǔ)充,在父窗口是frame的時(shí)候在刷新父窗口的時(shí)候會(huì)出現(xiàn)問題:
The page cannot be refreshed without resending the information.
后修改如下:
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
不需要執(zhí)行自帶的reload()方法,注意,不要再畫蛇添足加上這一句:
window.opener.parent.document.frames.item('mainFrame').location.reload();
========================================================================================
最后,為了同時(shí)支持刷新普通父窗口和frame父窗口,代碼如下:
function closeWin() {hasClosed = true;<%if(null != frame){%>window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;<%}else{%>window.opener.location = "javascript:reloadPage();";<%}%>//window.opener.top.mainFrame.location="javascript:reloadPage();";//self.opener.frames.mainFrame.location.reload(true);window.opener = null;window.close();}function window.onbeforeunload(){if (!hasClosed) {<%if(null != frame){%>window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;<%}else{%>window.opener.location = "javascript:reloadPage();";<%}%>window.opener = null;}}window.opener 的用法
window.opener 返回的是創(chuàng)建當(dāng)前窗口的那個(gè)窗口的引用,比如點(diǎn)擊了a.htm上的一個(gè)鏈接而打開了b.htm,然后我們打算在b.htm上輸入一個(gè)值然后賦予a.htm上的一個(gè)id為“name”的textbox中,就可以寫為:
window.opener.document.getElementById("name").value = "輸入的數(shù)據(jù)";
對(duì)于javascript中的window.opener沒有很好的理解。
為什么框架中不能使用,彈出窗口的父窗口不能在框架里面的某個(gè)頁面呢?那怎樣通過彈出窗口操作框架中的父窗口呢?
opener.parent.frames['frameName'].document.all.input1.value 試試這個(gè):)
frame框架里的頁面要改其他同框架下的頁面或父框架的頁面就用parent
window.opener引用的是window.open打開的頁面的父頁面。
window.frames對(duì)象可以引用iframe里的頁面,也可以引用frameset里的頁面.
可以這樣
window.frames[0].document.getElementById('xx');
可以這樣
window.frames[0].document.body.innerHTML;
frm = window.parent.window.frames['uploadFrame'];
frmDocument = frm.document;
frm.sb(3); //sb 是uploadFrame頁面里的一個(gè)函數(shù)
對(duì)于firefox
如果你遇到報(bào)錯(cuò):parent.document.frames has no properties
換為如下代碼就可以了,這個(gè)代碼IE,ff兼容. frm = window.parent.window.frames['uploadFrame'];其實(shí) frames 集合并不是掛在 document 而是掛在 window 對(duì)象下.
注意這樣修改frame里的頁面有限制,就是必須是同域下的,否則無法訪問
如果是同一域下,但是子域名不同,那么涉及到的js,html文件都加上一句。
document.domain = xxx.com [這里填寫你的域名]document.getElementById('iframeid').contentWindow.document.getElementById('someelementid');
問:
在父窗口window.open()一個(gè)子窗口。并定義一個(gè)變量i。
在子窗口輸入一個(gè)值j然后window.opener.i=j;
這樣能傳過去。但我在子窗口最后加了個(gè)window.close();就無法傳值了。
請(qǐng)問是否有辦法解決這個(gè)問題。使我傳遞值之后再關(guān)閉子窗口。
代碼如下:
父窗口:parent.jsp
<script>var i;window.open('<%=contextPath%>/usermanage/newscontrol/cd.jsp);</script><input type="button" onclick="alert(i)">子窗口:cd.jsp
<script>function subm(){window.opener.i=5;window.close();}</script><input type="button" onclick="subm()">最佳答案
你可以在父窗口放一個(gè)
<input id="fromChild" type="hidden" /><input type="button"onclick="alert(document.getElementById('fromChild').value)">在子窗口中:
function subm(){window.opener.document.getElementById('fromChild').value=5;window.close();}這樣既可
<head><script language=javascript>function windowclose(){ window.opener=null;window.close();}</script></head><body><input id="Button1" type="button" value="button" onclick="windowclose()" /></body>以上這篇js彈出窗口返回值的簡單實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注