alert();顯示一條消息,等待用戶關閉
confirm();顯示一條消息,等待用戶確定或取消
prompt();顯示一條消息,等待用戶輸入字符串並且返回字符串
showmodaldialog();
參數一:指定要打開的對話框的html頁面的url
參數二:指定任意值,對象或者數組都可以,在對話框中用window.dialogArguments獲取
參數三:包含以分號分割的鍵值對,設置對話框的大小及其他屬性
dialogwidth;dialogheight;
resizable=yes 允許對話框改變窗口大小
等到該對話窗口關閉之後
window.returnValue屬性的值就是該窗口關閉的返回值
這就要求對話框中的html必須包含確認按鈕,需要的話調用 window.close()
案列說明如下:
test.html頁面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--
作者:[email protected]
時間:2017-12-29
描述:
x,y,z如果不加單引號,就會報錯,因爲系統認爲是未定義的變量
-->
<div id="test">
<a href="javascript:showModalDialog('b.html',['進入3d繪圖區域','x','y','z'],'dialogwidth:400px;dialogheight:300px;resizable:no')">打開對話框</a>
</div>
<script type="text/javascript">
//showModalDialog("b.html",["進入3d繪圖區域","x","y","z"],"dialogwidth:400px;dialogheight:300px;resizable:no");
</script>
</body>
</html>
b.html頁面內容如下:
<form>
<fieldset id="fields"></fieldset>
<div style="text-align: center;">
<button onclick="okay()">確認</button>
<button onclick="window.close()">取消</button>
</div>
<script type="text/javascript">
var args=window.dialogArguments;//依據你傳過來的對象的不同而不同
var text="<legend>"+args[0]+"</legend>";
for (var i=1;i<args.length;i++)
//這裏要注意理解單引號與雙引號的作用
//注意對標籤的反斜槓忘記了
text+="<label>"+args[i]+":<input id='f"+i+"' /></label><br>"
document.getElementById("fields").innerHTML=text;
//設置取消按鈕
function cancel(){window.close();}
function okay(){
window.returnValue=[];
for (var i =1;i<args.length;i++) {
window.returnValue[i-1]=document.getElementById("f"+i).value;
document.write(window.returnValue);
window.close();
}
print(window.returnValue);
}
</script>
</form>
說明:這裏的window.close()在實際的應用中調用會彈出新窗口,而不能直接關閉當前窗口,後來經測試,該方法只兼容IE8(不含IE8)以前的,所以在使用的過程中要注意兼容性的問題
點擊鏈接showmodaldialog()方法介紹總結