DIV CSS 佈局教程網

jQuery 取得父視窗元素與父子視窗通訊完整解析
編輯:JavaScript綜合知識     

彈出視窗裡按下確定,要順便更新後面的主視窗——這個需求幾乎每個後台系統都會遇到。做法分兩大流派,取決於你用的是哪一種彈窗。

一、jQuery 怎麼選到別的視窗

關鍵是 jQuery 的第二個參數 context,它決定要在哪份文件裡搜尋:

$("#父視窗元素ID", window.parent.document);
// 等同於 JavaScript 的
window.parent.document.getElementById("父視窗元素ID");

$(selector, window.parent.document);        // 父視窗
$(selector, window.parent.parent.document); // 爺爺視窗
$(selector, window.top.document);           // 最頂層
$(selector, window.opener.document);        // 開啟我的那個視窗
$(selector, window.top.frames[0].document); // 頂層的第一個框架

把 window.parent 換成不同的視窗參照,就能往任意方向取值,其餘寫法大同小異。

二、開視窗的三種層次

window.open():真正的瀏覽器視窗

window.open(URL, windowName, parameters);

URL 是要開的位址,留空則只開空白窗;windowName 可用 _blank、_top 等內建名,語意等同 <a target>;parameters 則描述這個視窗的長相。

var newWindow = open('', '_blank',
  'width=400,height=100,menubar=no,toolbar=no,location=no,
   directories=no,status=no,scrollbars=yes,resizable=yes');

可用參數包含 top、left(距螢幕邊緣像素)、width、height,以及 menubar、toolbar、location、directories、scrollbars、status、resizable 等 yes/no 開關。

alert / confirm / prompt:只能顯示文字

這三個內建對話框功能單一,無法放入 HTML 控制項,要顯示多筆資料或表單就無能為力。

showModalDialog():能裝 HTML 的對話框

window.showModalDialog(sURL, vArguments, sFeatures)  // 模態
window.showModelessDialog(sURL, vArguments, sFeatures) // 非模態

兩者的差別在焦點:模態對話框永遠取得焦點、關閉前無法操作父視窗,且與開啟它的視窗維持連結,即使被其他視窗遮住也仍在;非模態則可以讓焦點移走,當開啟它的視窗網址改變時會自動關閉。

參數說明:

sURL(必選,字串)對話框要載入的文件位址。
vArguments(可選,變體)傳給對話框的參數,型別不限,包含陣列,對話框內用 window.dialogArguments 接收。
sFeatures(可選,字串)描述外觀,多項之間用分號隔開:

dialogHeight / dialogWidth:高度與寬度,不小於 100px。IE4 預設單位是 em,IE5 起是 px,建議一律明確寫 px。
dialogLeft / dialogTop:距桌面左側與上方的距離。
center:是否置中,預設 yes。
help:是否顯示說明按鈕,預設 yes。
resizable:可否縮放,預設 no(IE5+)。
status:是否顯示狀態列,模態預設 no、非模態預設 yes(IE5+)。
scroll:是否顯示捲軸,預設 yes,可為 yes/no/1/0/on/off。
另有 dialogHide、edge(sunken 或 raised)、unadorned 三個屬性主要用於 HTA,一般網頁較少使用。

vArguments 傳字串時上限約 4096 字元,也可以直接傳物件。

三、子視窗怎麼跟父視窗講話

用 window.open() 開的:走 opener

window.opener.location.reload();       // 子視窗刷新父視窗
window.opener.location.href            // 取得父視窗網址
window.opener.location.pathname        // 取得父視窗路徑名
window.location.href = window.location.href; // 重新定位自己
window.location.reload;

用 showModalDialog() 開的:必須把 window 當參數傳進去

模態子視窗拿不到 window.opener,所以要在開窗時就把父視窗交給它:

// 父視窗中
var newWin = window.showModalDialog(url, window, '');
newWin.open();

// 子視窗中,先取回父視窗物件
var parent = window.dialogArguments;

拿到之後,就能直接操作父視窗的表單與元素:

// 提交父視窗的表單
var parent = window.dialogArguments;
parent.document.form1.action = "QueryInfor.jsp";
parent.document.form1.submit();

// 刷新父頁
var parent = window.dialogArguments;
parent.location.reload();

// 直接改父視窗某個欄位的值
var parent = window.dialogArguments;
var x = parent.document.getElementById("age").value;
parent.document.getElementById("age").value = Number(x) + 1;

用返回值把資料交回去

如果不想直接改父視窗,也可以用 window.returnValue:

// 子視窗中
var parent = window.dialogArguments;
var x = parent.document.getElementById("age").value;
window.returnValue = Number(x) + 1;

// 父視窗中接收
var newWin = window.showModalDialog(url, window, '');
if (newWin != null) {
  document.getElementById("age").value = newWin;
}

該選哪一種

直接設定父視窗元素的值,寫起來最直接也最靈活;用 returnValue 回傳則耦合度低、資料流向清楚。挑哪一種取決於既有架構——硬套不合適的方法,不只拖慢開發,也讓程式風格變得難以維護。

初次接觸這兩套 API 確實容易搞混,但只要把「誰開了誰」和「資料要往哪個方向流」這兩件事想清楚,就不難理解。

必要提醒

showModalDialog 與 showModelessDialog 皆為 IE 專屬,且已從主流瀏覽器移除。新專案請改用 <dialog> 元素搭配 showModal(),或用 React、Vue 的 Modal 元件;父子溝通則統一走 postMessage,這也是跨來源情境下唯一正確的方式。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved