DIV CSS 佈局教程網

jQuery 取得父視窗元素與父子視窗通訊完整解析
編輯:JQuery特效代碼     

在開發多視窗頁面時,取得父視窗元素以及父子視窗之間的通訊,是非常常見的需求。本文整理了 jQuery 與原生 JavaScript 的對應寫法,並說明各種彈出視窗方式的差異。

一、取得父視窗的元素

使用 jQuery 時,只要在第二個參數傳入 window.parent.document 即可:

$("#父視窗元素ID", window.parent.document);

對應的原生 JavaScript 寫法為:

window.parent.document.getElementById("父視窗元素ID");

以此類推,取得上層視窗的元素可寫成:

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

二、建立子視窗的幾種方式

1. 使用 window.open()

open() 會產生一個新的視窗物件,用法如下:

window.open(URL, windowName, parameters);

其中 URL 為要開啟的位址(留空則不載入任何頁面);windowName 為視窗名稱,可使用 _top、_blank 等內建名稱,與 <a target> 的意義相同;parameters 則描述視窗的外觀與參數。例如開啟一個 400×100 的乾淨視窗:

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,分別控制選單列、工具列、網址列、連結區、捲軸、狀態列與是否可縮放。

2. 使用對話框

除了 open(),也可以透過 alert()、confirm()、prompt() 彈出對話框。不過這些方式功能較單一,難以在對話框中呈現多筆資料或 HTML 控件。

3. 使用模態/非模態對話框

若需要在對話框中顯示 HTML 內容,可使用:

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

兩者的差異在於:非模態視窗不必用 window.close() 關閉,開啟後原視窗仍可操作,對話框不永遠處於焦點;模態對話框則始終擁有焦點,直到關閉為止,並與開啟它的視窗保持連結關係。

參數說明:sURL 為必選,指定要顯示的文件位址;vArguments 為可選,用來向對話框傳參,型別不限,對話框透過 window.dialogArguments 取得;sFeatures 為可選,描述外觀,以分號分隔,包含 dialogHeight、dialogWidth、dialogLeft、dialogTop、center、help、resizable、status、scroll 等。

傳值範例:

var newWin = window.showModalDialog(url, window, 'dialogHeight:500px, dialogLeft:100px, dialogTop:100px, dialogWidth:300px, status:0, edge:sunken');
newWin.open();

三、子視窗與父視窗的通訊

1. window.open() 建立的視窗

可在子視窗中透過 window.opener 取得父視窗物件,進而執行重新整理、取值等操作:

window.opener.location.reload();      // 子視窗重新整理父視窗
window.opener.location.href           // 取得父視窗 href
window.opener.location.pathname       // 取得父視窗路徑名

2. 模態視窗與父視窗通訊

以 showModalDialog() 建立的視窗無法用 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.submit();

若要從子視窗回傳值給父視窗,需使用 window.returnValue。子視窗中:

var parent = window.dialogArguments;
var x = parent.document.getElementById("age").value;
x = x + 1;
window.returnValue = x;

父視窗中接收:

var newWin = window.showModalDialog(url, window, '');
if (newWin != null) document.getElementById("age").value = newWin;

當然,也可以在子視窗中直接設定父視窗元素的值,這樣往往更為直觀靈活:

var parent = window.dialogArguments;
var x = parent.document.getElementById("age").value;
x = x + 1;
parent.document.getElementById("age").value = x;

以上,是整理自實務應用的父子視窗通訊方法。不論使用 window.open() 還是 showModalDialog(),雖然實作細節差異不小,初學時容易感到混亂,但只要理清父子視窗之間的角色關係,就能輕鬆掌握。

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