showModalDialog 需要 IE 4 以上、showModelessDialog 需要 IE 5 以上。兩者皆為 IE 專屬 API,其他瀉覽器不支援。
showModalDialog:打開後始終保持輸入焦點,無法操作主視窗,除非先關閉對話框(強制模態)。showModelessDialog:打開後使用者可以自由切換焦點,只是主視窗被對話框擋住(非強制模態)。vReturnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures])
vReturnValue = window.showModelessDialog(sURL [, vArguments] [, sFeatures])
sURL(必選,字串):對話框要顯示的文件 URL。
vArguments(可選,變體):傳給對話框的參數,型別不限,包含陣列。對話框內部透過 window.dialogArguments 取得。
sFeatures(可選,字串):描述對話框外觀,可用一項或多項,以分號隔開:
dialogHeight:對話框高度,不小於 100px。IE 4 中預設單位是 em,IE 5 起改為 px。dialogWidth:對話框寬度。dialogLeft / dialogTop:距螢幕左側/上方的距離。center:{yes|no|1|0} 是否置中,預設 yes,仍可指定高寬。help:是否顯示說明按鈕,預設 yes。resizable(IE5+):是否可縮放,預設 no。status(IE5+):是否顯示狀態列,模態預設 no、非模態預設 yes。scroll:{yes|no|1|0|on|off} 是否顯示捲動軸,預設 yes。dialogHide:列印或列印預覽時是否隱藏,預設 no。edge:{sunken|raised} 邊框樣式,預設 raised。unadorned:{yes|no|1|0|on|off} 預設 no。主視窗 f.html:
<html>
<head>
<title>主視窗</title>
<script type="text/javascript">
var child;
function openDialogBox() {
child = window.showModalDialog('c.html', document.all["txt"],
"dialogWidth=500px;dialogHeight=200px;");
}
</script>
</head>
<body>
<input name="txt" type="text" disabled="disabled" />
<input name="btn" type="button" value="打開對話框" onClick="openDialogBox();" />
</body>
</html>
對話框 c.html:
<html>
<head>
<title>對話框</title>
<script type="text/javascript">
function set() {
window.dialogArguments.value = document.all["txt"].value;
}
</script>
</head>
<body>
<input name="txt" type="text" />
<input name="btn" type="button" value="設置" onClick="set();" />
</body>
</html>
這個範例示範了雙向溝通:主視窗把 txt 元素當成 vArguments 傳入,對話框則直接改寫該元素的 value,關閉後主視窗即可看到結果。
實務提醒:這組 API 已不適用於現代瀉覽器,跨瀉覽器專案應改用 <dialog> 元素搭配 showModal(),或自行以 CSS 遮罩實作模態視窗。