js彈出窗口返回值的簡單實(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 的用法在一般的用法中,只是用來解決關(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è)參考,也希望大家多多支持腳本之家。
相關(guān)文章
多瀏覽器兼容的動(dòng)態(tài)加載 JavaScript 與 CSS
Omar AL Zabir這位MVP總是喜歡搞些稀奇古怪同時(shí)又很實(shí)用的小東西,并且還十分值得參考。最近他就做了一個(gè)叫做ensure的小工具用于動(dòng)態(tài)加載JavaScript、CSS與HTML,而且IE、Firefox、Opera、Safari都支持了,那么我們就來看看ensure是如何做到動(dòng)態(tài)加載JavaScript與CSS的。2008-09-09
前端數(shù)組去重面試我會(huì)問這3個(gè)小問題
數(shù)組去重在我們的前端的面試過程中經(jīng)過會(huì)遇到,有一些人可能會(huì)想到一兩種,但是數(shù)據(jù)去重的算法真的太多了,下面這篇文章主要給大家介紹了關(guān)于前端數(shù)組去重面試3個(gè)小問題的相關(guān)資料,需要的朋友可以參考下2023-01-01
JS查找字符串中出現(xiàn)最多的字符及個(gè)數(shù)統(tǒng)計(jì)
最近在項(xiàng)目中遇到這樣的需求:求字符串'nininihaoa'中出現(xiàn)次數(shù)最多字符。怎么實(shí)現(xiàn)呢?下面小編給大家分享具體實(shí)現(xiàn)代碼,需要的朋友參考下吧2017-02-02
微信小程序?qū)崿F(xiàn)滑動(dòng)翻頁效果(完整代碼)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)滑動(dòng)翻頁效果,本文通過效果圖展示實(shí)例代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
JS+CSS實(shí)現(xiàn)消息的點(diǎn)擊展示和隱藏(H5端)
在 H5 端,我們經(jīng)常需要實(shí)現(xiàn)類似于點(diǎn)擊按鈕來展示或隱藏消息的功能,以下是一個(gè)使用 CSS 和 JavaScript(配合 Vue.js)來實(shí)現(xiàn)這個(gè)效果的簡單示例,需要的朋友可以參考下2023-10-10
頁面加載完后自動(dòng)執(zhí)行一個(gè)方法的js代碼
這篇文章主要介紹了加載完成一個(gè)頁面后自動(dòng)執(zhí)行一個(gè)方法,很簡單很實(shí)用,需要的朋友可以參考下2014-09-09

