語法:
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 |
參數傳遞方式:
父視窗
向子視窗傳遞參數採用ShowModalDialog的第2個參數(vArguments)即可,父視窗要獲取子視窗傳回的參數則可通過ShowModalDialog函數的返回值(vReturnValue )獲取。
子視窗
取得父視窗參數的方法,只要在子視窗裡下Javascript,用window物件的dialogArguments屬性來取取,例如:
var a=window.dialogArguments;
子視窗向父視窗返回參數,可用window.returnValue屬性,如:
window.returnValue=1;
window.close();
範例
父視窗
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
//亂數
function fnRandom(iModifier) {
return parseInt(Math.random() * iModifier);
}
//設定打開視窗的參數
function fnSetValues() {
var iHeight = oForm.oHeight.options[
oForm.oHeight.selectedIndex].text;
if (iHeight.indexOf("Random") > -1) {
iHeight = fnRandom(document.body.clientHeight);
}
var sFeatures = "dialogHeight: " + iHeight + "px;";
return sFeatures;
}
function fnOpen() {
//呼叫設定參數涵式
var sFeatures = fnSetValues();
//傳遞參數
var sDialogArguments = document.getElementById("txtParent").value;
//打開視窗
var retValue = window.showModalDialog("child.html", sDialogArguments,
sFeatures);
//顯示返回值
//alert(retValue);
var divElement = document.getElementById("divChild");
divElement.innerHTML = retValue;
}
</script>
<title></title>
</head>
<body>
<form name="oForm">
Dialog Height
<select name="oHeight">
<option>-- Random --</option>
<option>150</option>
<option>200</option>
<option>250</option>
<option>300</option>
</select>
Create Modal Dialog Box<br>
本視窗(父):<input id="txtParent" type="text" />
<input type="button" value="打開視窗" onclick="fnOpen()"><br>
來自子視窗:<div id="divChild"> 1</div>
</form>
</body>
</html>
子視窗
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
//接收父視窗傳值
function setValue() {
document.getElementById("divFromParent").innerHTML = window.dialogArguments;
}
//返回父視窗,並回傳值
function retParantWin() {
//alert(document.getElementById("txtChild").value);
window.returnValue = document.getElementById("txtChild").value;
window.close();
}
</script>
<title></title>
</head>
<body onload="setValue()">
<form>
Child <br/>
父視窗傳值:<div id="divFromParent"> 1</div>
本視窗(子)輸入:<input id="txtChild" type="text" />
<input id="Button1" type="button" value="返回父視窗" onclick="retParantWin();"/>
</form>
</body>
</html>
參考
http://msdn.microsoft.com/en-us/library/ie/ms536759(v=vs.85).aspx
opening popup window by avoiding popup blocker
Avoid Browser Pop-up Blockers
http://blog.longwin.com.tw/2007/08/javascript_set_id_style_method_2007/
http://blog.darkthread.net/post-2011-07-12-showmodaldialog-and-popup-blocker.aspx
大大好,在Chrome下會失效
回覆刪除有可以通用的idea嗎…(思考中)
div 可以通用
回覆刪除