js彈出窗口返回值的簡(jiǎn)單實(shí)例
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 的用法在一般的用法中,只是用來(lái)解決關(guān)閉窗口時(shí)不提示彈出窗口, 而對(duì)它更深層的了解一般比較少。其 實(shí) window.opener是指調(diào)用window.open方法的窗口。
在工作中主要是用來(lái)解決部分提交的。這種跨頁(yè)操作對(duì)工作是非常有幫助的。
如果你在主窗口打開(kāi)了一個(gè)頁(yè)面,并且希望主窗口刷新就用這個(gè),打開(kāi)頁(yè)面的window.opener就相當(dāng)于
主窗口的window。
主窗口的刷新你可以用
window.opener.location.reload();
如果你用虛擬的目錄:如struts的*.do會(huì)提示你重試
你可以改成這樣 window.opener.yourformname.submit()
就好了
2〉
在應(yīng)用中有這樣一個(gè)情況,
在A窗口中打開(kāi)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ò)誤,說(shuō)缺少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)問(wè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è)鏈接而打開(kāi)了b.htm,然后我們打算在b.htm上輸入一個(gè)值然后賦予a.htm上的一個(gè)id為“name”的textbox中,就可以寫為:
window.opener.document.getElementById("name").value = "輸入的數(shù)據(jù)";
對(duì)于javascript中的window.opener沒(méi)有很好的理解。
為什么框架中不能使用,彈出窗口的父窗口不能在框架里面的某個(gè)頁(yè)面呢?那怎樣通過(guò)彈出窗口操作框架中的父窗口呢?
opener.parent.frames['frameName'].document.all.input1.value 試試這個(gè):)
frame框架里的頁(yè)面要改其他同框架下的頁(yè)面或父框架的頁(yè)面就用parent
window.opener引用的是window.open打開(kāi)的頁(yè)面的父頁(yè)面。
window.frames對(duì)象可以引用iframe里的頁(yè)面,也可以引用frameset里的頁(yè)面.
可以這樣
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頁(yè)面里的一個(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里的頁(yè)面有限制,就是必須是同域下的,否則無(wú)法訪問(wèn)
如果是同一域下,但是子域名不同,那么涉及到的js,html文件都加上一句。
document.domain = xxx.com [這里填寫你的域名]document.getElementById('iframeid').contentWindow.document.getElementById('someelementid');
問(wèn):
在父窗口window.open()一個(gè)子窗口。并定義一個(gè)變量i。
在子窗口輸入一個(gè)值j然后window.opener.i=j;
這樣能傳過(guò)去。但我在子窗口最后加了個(gè)window.close();就無(wú)法傳值了。
請(qǐng)問(wèn)是否有辦法解決這個(gè)問(wèn)題。使我傳遞值之后再關(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彈出窗口返回值的簡(jiǎn)單實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
javascript 密碼強(qiáng)弱度檢測(cè)萬(wàn)能插件
網(wǎng)上用的比較多的一種用來(lái)檢測(cè)用戶輸入密碼的強(qiáng)度檢測(cè),其實(shí)就是把一些常用的拼音,英文單詞, 純數(shù)字,純字母等。2009-02-02淺談js繼承的實(shí)現(xiàn)及公有、私有、靜態(tài)方法的書寫
下面小編就為大家?guī)?lái)一篇淺談js繼承的實(shí)現(xiàn)及公有、私有、靜態(tài)方法的書寫。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10javascript取消文本選定的實(shí)現(xiàn)代碼
最近在做拖動(dòng)布局. 發(fā)現(xiàn)有文本選定的時(shí)候, 進(jìn)行拖動(dòng)很不好看.2010-11-11JavaScript使用atan2來(lái)繪制箭頭和曲線的實(shí)例
下面小編就為大家?guī)?lái)一篇JavaScript使用atan2來(lái)繪制箭頭和曲線的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09js模仿微信朋友圈計(jì)算時(shí)間顯示幾天/幾小時(shí)/幾分鐘/幾秒之前
本篇文章主要介紹了js模仿微信朋友圈計(jì)算時(shí)間顯示幾天/幾小時(shí)/幾分鐘/幾秒之前的實(shí)例。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04使用微信小程序開(kāi)發(fā)彈出框應(yīng)用實(shí)例詳解
本文通過(guò)實(shí)例代碼給大家介紹了使用微信小程序開(kāi)發(fā)彈出框功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10javascript游戲開(kāi)發(fā)之《三國(guó)志曹操傳》零部件開(kāi)發(fā)(五)可移動(dòng)地圖的實(shí)現(xiàn)
首先來(lái)說(shuō),我對(duì)游戲開(kāi)發(fā)可以算是不怎么深入,因?yàn)楝F(xiàn)在的程序員愛(ài)用canvas,我卻就只會(huì)拿幾個(gè)div湊和。不過(guò)沒(méi)關(guān)系,因?yàn)樽龀鰜?lái)的同樣是游戲。哈!廢話最近有點(diǎn)多,感興趣的朋友可以了解下2013-01-01JavaScript通過(guò)prototype給對(duì)象定義屬性用法實(shí)例
這篇文章主要介紹了JavaScript通過(guò)prototype給對(duì)象定義屬性用法,實(shí)例分析了prototype的功能及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03