【JavaScript】showModalDialog 應用
showModalDialog 應用於資料檢查或需要呼叫其他頁面(Ex︰Asp、Javascript)來處理事情,最後回傳結果值。對於不會使用AJAX的人來說,是一個不錯的作法!
當然可以的話,用AJAX方式,是最好不過!
語法:
vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures])
參數說明:
sURL
必要參數,類型:字串。用來指定對話方塊要載入網頁的URL。
vArguments
非必要參數,類型:變動。用來向對話方塊傳遞參數。傳遞的參數類型不限,包括陣列等。對話方塊通過window.dialogArguments來取得傳遞進來的參數。
sFeatures
非必要參數,類型:字串。用來描述對話方塊的外觀等資訊,可以使用以下的一個或多個,用分號“;”隔開。
參數傳遞方式:
父視窗
向子視窗傳遞參數採用ShowModalDialog的第2個參數(vArguments)即可,父視窗要獲取子視窗傳回的參數則可通過ShowModalDialog函數的返回值(vReturnValue )獲取。
子視窗
取得父視窗參數的方法,只要在子視窗裡下Javascript,用window物件的dialogArguments屬性來取,例如:
var a=window.dialogArguments;
子視窗向父視窗返回參數,可用window.returnValue屬性,如:
window.returnValue=1;
window.close();
假設網頁傳送前,檢查是否有附件,若無附件不允許傳送。父頁面(Ex︰A.asp),子頁面(Ex︰B.asp)。
父頁面(Ex︰A.asp)
Javascript:
<script LANGUAGE="JavaScript">
//資料檢查
function check_data()
{
//判斷上傳附件
if (upload_check()==false)
{
alert("請檢附文件依據!");
return false;
}
}
//判斷上傳附件
function upload_check()
{
var testresults; //結果值
//呼叫處理網頁-子頁面(Ex︰B.asp)
var url = "/public/B.asp?flow_formid=AdjWork&flow_formno="+document.getElementsByName("formno")[0].value;
//attchcount存放回傳結果的附件筆數
//"dialogWidth=0px;dialogHeight=0px" 視窗大小為0
var attchcount = window.showModalDialog(url,"","dialogWidth=0px;dialogHeight=0px" );
if (attchcount == "0")
{
testresults=false;
}else{
testresults=true;
}
return (testresults);
}
</script>
HTML︰
<form name="form1" method="post" action="xxx_f000.asp" onsubmit="return check_data();">
=========================================================================
子頁面(Ex︰B.asp)ASP程式省略
Javascript:
<script languag=javascript>
//處理結果回傳給父頁面
window.returnValue = "<%=R_counts%>";
window.close();
</script>
參考資源:
http://trufflepenne.blogspot.tw/2013/05/javascript-showmodaldialog.html
使用javascript ShowModalDialog
留言
張貼留言