js彈出窗口返回值的簡單實例
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>
補充:window.opener 的用法
window.opener 的用法在一般的用法中,只是用來解決關(guān)閉窗口時不提示彈出窗口, 而對它更深層的了解一般比較少。其 實 window.opener是指調(diào)用window.open方法的窗口。
在工作中主要是用來解決部分提交的。這種跨頁操作對工作是非常有幫助的。
如果你在主窗口打開了一個頁面,并且希望主窗口刷新就用這個,打開頁面的window.opener就相當于
主窗口的window。
主窗口的刷新你可以用
window.opener.location.reload();
如果你用虛擬的目錄:如struts的*.do會提示你重試
你可以改成這樣 window.opener.yourformname.submit()
就好了
2〉
在應(yīng)用中有這樣一個情況,
在A窗口中打開B窗口,在B窗口中操作完以后關(guān)閉B窗口,同時自動刷新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窗口的時候會提示錯誤,說缺少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)閉和強制關(guān)閉窗口時能捕捉到事件,用了全局變量hasClosed
==============================================
補充,在父窗口是frame的時候在刷新父窗口的時候會出現(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();
========================================================================================
最后,為了同時支持刷新普通父窗口和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)建當前窗口的那個窗口的引用,比如點擊了a.htm上的一個鏈接而打開了b.htm,然后我們打算在b.htm上輸入一個值然后賦予a.htm上的一個id為“name”的textbox中,就可以寫為:
window.opener.document.getElementById("name").value = "輸入的數(shù)據(jù)";
對于javascript中的window.opener沒有很好的理解。
為什么框架中不能使用,彈出窗口的父窗口不能在框架里面的某個頁面呢?那怎樣通過彈出窗口操作框架中的父窗口呢?
opener.parent.frames['frameName'].document.all.input1.value 試試這個:)
frame框架里的頁面要改其他同框架下的頁面或父框架的頁面就用parent
window.opener引用的是window.open打開的頁面的父頁面。
window.frames對象可以引用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頁面里的一個函數(shù)
對于firefox
如果你遇到報錯:parent.document.frames has no properties
換為如下代碼就可以了,這個代碼IE,ff兼容. frm = window.parent.window.frames['uploadFrame'];其實 frames 集合并不是掛在 document 而是掛在 window 對象下.
注意這樣修改frame里的頁面有限制,就是必須是同域下的,否則無法訪問
如果是同一域下,但是子域名不同,那么涉及到的js,html文件都加上一句。
document.domain = xxx.com [這里填寫你的域名]document.getElementById('iframeid').contentWindow.document.getElementById('someelementid');
問:
在父窗口window.open()一個子窗口。并定義一個變量i。
在子窗口輸入一個值j然后window.opener.i=j;
這樣能傳過去。但我在子窗口最后加了個window.close();就無法傳值了。
請問是否有辦法解決這個問題。使我傳遞值之后再關(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()">
最佳答案
你可以在父窗口放一個
<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彈出窗口返回值的簡單實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談js繼承的實現(xiàn)及公有、私有、靜態(tài)方法的書寫
下面小編就為大家?guī)硪黄獪\談js繼承的實現(xiàn)及公有、私有、靜態(tài)方法的書寫。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10js模仿微信朋友圈計算時間顯示幾天/幾小時/幾分鐘/幾秒之前
本篇文章主要介紹了js模仿微信朋友圈計算時間顯示幾天/幾小時/幾分鐘/幾秒之前的實例。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04javascript游戲開發(fā)之《三國志曹操傳》零部件開發(fā)(五)可移動地圖的實現(xiàn)
首先來說,我對游戲開發(fā)可以算是不怎么深入,因為現(xiàn)在的程序員愛用canvas,我卻就只會拿幾個div湊和。不過沒關(guān)系,因為做出來的同樣是游戲。哈!廢話最近有點多,感興趣的朋友可以了解下2013-01-01JavaScript通過prototype給對象定義屬性用法實例
這篇文章主要介紹了JavaScript通過prototype給對象定義屬性用法,實例分析了prototype的功能及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03