Javascript showModalDialog兩個窗體之間傳值
2024-05-06 14:13:47
供稿:網友
 
Javascript 兩個窗體之間傳值實現代碼
javascript中還有一個函數window.showModalDialog也可以打開一個新窗體,不過他打開的是一個模態窗口,那么如何在父窗體和子窗體之間傳值呢?我們先看該函數的定義:vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures]) 
參數說明: 
sURL--必選參數,類型:字符串。用來指定對話框要顯示的文檔的URL。 
vArguments--可選參數,類型:變體。用來向對話框傳遞參數。傳遞的參數類型不限,包括數組等。對話框通過window.dialogArguments來取得傳遞進來的參數。 
sFeatures--可選參數,類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開。 
dialogHeight :對話框高度,不小于100px,IE4中dialogHeight 和 dialogWidth 默認的單位是em,而IE5中是px,為方便其見,在定義modal方式的對話框時,用px做單位。 
dialogWidth: 對話框寬度。 
dialogLeft: 離屏幕左的距離。 
dialogTop: 離屏幕上的距離。 
center: {yes | no | 1 | 0 }:窗口是否居中,默認yes,但仍可以指定高度和寬度。 
help: {yes | no | 1 | 0 }:是否顯示幫助按鈕,默認yes。 
resizable: {yes | no | 1 | 0 } [IE5+]:是否可被改變大小。默認no。 
status: {yes | no | 1 | 0 } [IE5+]:是否顯示狀態欄。默認為yes[ Modeless]或no[Modal]。 
scroll:{ yes | no | 1 | 0 | on | off }:指明對話框是否顯示滾動條。默認為yes。 
如:"dialogWidth=200px;dialogHeight=100px" 
因此我們可以通過window.dialogArguments參數來在兩個窗體之間傳值 
如下面兩個頁面:FatherPage.htm: 
 代碼如下: 
<script type="text/javascript"> 
function OpenChildWindow() 
{ 
window.showModalDialog('ChildPage.htm',document.getElementById('txtInput').value); 
} 
</script> 
<input type="text" id="txtInput" /> 
<input type="button" value="OpenChild" onclick="OpenChildWindow()" /> 
 
ChildPage.htm: 
 代碼如下: 
<body onload="Load()"> 
<script type="text/javascript"> 
function Load() 
{ 
document.getElementById('txtInput').value=window.dialogArguments ; 
} 
</script> 
<input type="text" id="txtInput" /> 
</body> 
 
上面只是傳遞簡單的字符串,我們還可以傳遞數組,如:FatherPage.htm: 
XML-Code: 
 代碼如下: 
<script type="text/javascript"> 
function OpenChildWindow() 
{ 
var args = new Array(); 
args[0] = document.getElementById('txtInput').value; 
window.showModalDialog('ChildPage.htm',args); 
} 
</script> 
<input type="text" id="txtInput" /> 
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />ChildPage.htm: 
XML-Code: 
<script type="text/javascript"> 
function Load() 
{ 
document.getElementById('txtInput').value=window.dialogArguments[0] ;