本範例可以實現彈出一個模態視窗,並示範了兩種接收和傳遞參數的方法,同時可以接受模態視窗返回的多個變數
showModalDialog協助可以參閱微軟MSDN http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/showmodaldialog.asp
AA.HTM
--------------------------------------------------------------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>主介面</title>
</head>
<body>
<form id="getForm"> 回傳值:<input type="text" id="getData" readOnly> <input type="text" id="getData1" readOnly>
</form>
<input type="button" value="開啟新視窗1" onClick="openWin1()">
<input type="button" value="開啟新視窗2" onClick="openWin2()">
<script language="JavaScript">
function openWin1()
{
var srcFile = "BB.htm"; //新視窗的文檔名稱
var winFeatures = "dialogHeight:300px; dialogLeft:200px;";
var obj = getForm; //將form作為對象傳遞給新視窗
window.showModalDialog(srcFile, obj, winFeatures);
}
function openWin2()
{
var srcFile = "CC.htm"; //新視窗的文檔名稱
var winFeatures = "dialogHeight:300px; dialogLeft:200px;";
var obj = getForm.getData.value; //將form作為對象傳遞給新視窗
var str=window.showModalDialog(srcFile, obj, winFeatures);
if(str!=null)
document.getForm.getData.value = str;
}
</script>
</body>
</html>
BB.HTM
重要提示:你快顯視窗中調用的是ASPX檔案時,必須在Page_Load事件中加入以下代碼
Response.Expires = 0; Response.Cache.SetNoStore(); Response.AppendHeader("Pragma", "no-cache");
否則,每次彈出的頁面都是上一次的Cache,不會改變。
------------------------------------------------------------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新視窗BB</title>
</head>
<base target="_self"> <!--這句話非常重要,只要加上才能保證在快顯視窗中調用服務端代碼而不會再彈出一個新視窗-->
<body>
<form id="sendForm"> 請輸入資料:
<input type="text" id="writeData">
<input type="text" id="writeData1">
<input type="button" value="送回父視窗" onClick="send(sendForm.writeData.value,sendForm.writeData1.value);">
</form>
<script language="JavaScript">
function send(val,val1)
{
//取得父視窗傳過來的對象
var myObj = window.dialogArguments;
//賦值
myObj.getData.value = val;
myObj.getData1.value = val1;
//關閉視窗
window.close();
}
</script>
</body>
</html>
CC.HTM
------------------------------------------------------------------------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新視窗CC</title>
</head>
<base target="_self"> <!--這句話非常重要,只要加上才能保證在快顯視窗中調用服務端代碼而不會再彈出一個新視窗-->
<body>
<form id="sendForm"> 請輸入資料:
<input type="text" id="writeData">
<input type="button" value="送回父視窗" onClick="send(sendForm.writeData.value);">
</form>
<script language="JavaScript">
function send(val)
{
window.returnValue=val
window.close();
}
</script>
</body>
</html>