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 的用法在一般的用法中,只是用來解決關閉窗口時不提示彈出窗口, 而對它更深層的了解一般比較少。其 實 window.opener是指調(diào)用window.open方法的窗口。
在工作中主要是用來解決部分提交的。這種跨頁操作對工作是非常有幫助的。
如果你在主窗口打開了一個頁面,并且希望主窗口刷新就用這個,打開頁面的window.opener就相當于
主窗口的window。
主窗口的刷新你可以用
window.opener.location.reload();
如果你用虛擬的目錄:如struts的*.do會提示你重試
你可以改成這樣 window.opener.yourformname.submit()
就好了
2〉
在應用中有這樣一個情況,
在A窗口中打開B窗口,在B窗口中操作完以后關閉B窗口,同時自動刷新A窗口
function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.close();
}
function window.onbeforeunload(){
if(!hasClosed){
window.opener.location="javascript:reloadPage();";
}
}
</script>
上面的代碼在關閉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:由于需要支持正常關閉和強制關閉窗口時能捕捉到事件,用了全局變量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();就無法傳值了。
請問是否有辦法解決這個問題。使我傳遞值之后再關閉子窗口。
代碼如下:
父窗口: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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
多瀏覽器兼容的動態(tài)加載 JavaScript 與 CSS
Omar AL Zabir這位MVP總是喜歡搞些稀奇古怪同時又很實用的小東西,并且還十分值得參考。最近他就做了一個叫做ensure的小工具用于動態(tài)加載JavaScript、CSS與HTML,而且IE、Firefox、Opera、Safari都支持了,那么我們就來看看ensure是如何做到動態(tài)加載JavaScript與CSS的。2008-09-09
JS查找字符串中出現(xiàn)最多的字符及個數(shù)統(tǒng)計
最近在項目中遇到這樣的需求:求字符串'nininihaoa'中出現(xiàn)次數(shù)最多字符。怎么實現(xiàn)呢?下面小編給大家分享具體實現(xiàn)代碼,需要的朋友參考下吧2017-02-02

