父頁 a.html 開啟子頁 b.html,子頁填好內容後把值寫回父頁的輸入框,再關閉自己。
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">
它指向「開啟當前視窗的那個視窗」。多數人只知道它能用來避免關閉時跳出提示,實際上它的價值在跨頁操作:在子頁中,window.opener 就等同於主視窗的 window,因此可以:
window.opener.document.getElementById("name").value = "輸入的數據";
window.opener.location.reload(); // 重新整理主視窗
window.opener.yourformname.submit(); // 提交主視窗的表單
若主視窗用了 *.do 這類虛擬路徑,直接 reload 可能觸發重新提交提示,改呼叫表單的 submit 通常就能避開。
需求是:A 開 B,B 操作完關閉,同時自動刷新 A。實作時要區分「使用者按確定關閉」與「直接叉掉視窗」兩種路徑,因此用一個全域旗標記錄:
function closeWin() {
hasClosed = true;
window.opener.location = "javascript:reloadPage();";
window.opener = null; // 關鍵:先斷開引用再關閉
window.close();
}
window.onbeforeunload = function () {
if (!hasClosed) { // 已走過 closeWin 就不再重複處理
window.opener.location = "javascript:reloadPage();";
}
};
function reloadPage() {
history.go(0);
document.execCommand("refresh");
document.location = document.location;
document.location.reload();
}
少了 window.opener = null 這一行,關閉時會出現「缺少 Object」的錯誤。原因是視窗正在銷毀,此時再去存取 opener 的屬性已經不安全;先清空引用是比較穩妥的順序。
若父頁被包在 frameset 裡,重新整理會跳出「頁面需要重新傳送資訊才能重新整理」的提示。改寫如下:
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
注意這裡不要再補一句 ...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 = null;
window.close();
}
window.onbeforeunload = function () {
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.frames 可以引用 iframe 或 frameset 中的頁面,注意 frames 集合是掛在 window 而非 document 上(寫成 parent.document.frames 在 Firefox 會報 has no properties):
window.frames[0].document.getElementById('xx');
window.frames[0].document.body.innerHTML;
frm = window.parent.window.frames['uploadFrame'];
frmDocument = frm.document;
frm.sb(3); // 呼叫 uploadFrame 頁面中的函數
同源限制無法迴避。修改 frame 內頁面必須同域,否則瀏覽器直接阻擋。若是同域但子網域不同,雙方文件都要加上:
document.domain = "xxx.com"; // 填入你的網域
有個問題常被提出:父視窗宣告變數 i,子視窗執行 window.opener.i = 5 本來能傳值,但若在後面加上 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();
}
或者更保守一點,關閉前先清空引用:
function windowclose() {
window.opener = null;
window.close();
}
差別在於:寫入 DOM 屬性是一次完成的操作,瀏覽器較不容易在銷毀過程中把更動丟掉;直接改父視窗的變數則依賴時序,不夠可靠。