web模態窗口window.showModalDialog簡介

1、基本知識

showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog()方法用來創建一個顯示HTML內容的模態對話框。 window.showModelessDialog()方法用來創建一個顯示HTML內容的非模態對話框。
    2、使用方法
vReturnValue=window.showModalDialog(sURL[,vArguments][,sFeatures]); vReturnValue=window.showModelessDialog(sURL[,vArguments][,sFeatures]);
    3、參數說明
參數名稱 性質 類型 作用
sURL 必選 字符串 用來指定對話框要顯示的網頁的URL。
vArguments 可選 變體 用來向對話框傳遞參數。參數類型不限。對話框通過window.dialogArguments來取得傳遞進來的參數。
sFeatures 可選 字符串 用來描述對話框的外觀等信息
    4、sFeatures參數說明
參數名稱 參數屬性 說明
dialogHeight npx 對話框高度,不小於100px
dialogWidth npx 對話框寬度
dialogLeft npx 離主窗口左的距離
dialogTop npx 離主窗口上的距離
center {yes | no | 1 | 0 } 窗口是否居中,默認yes
help {yes | no | 1 | 0 } 是否顯示幫助按鈕,默認yes
resizable {yes | no | 1 | 0 } 是否可改變大小,默認no
status {yes | no | 1 | 0 } 是否顯示狀態欄,默認爲yes[ Modeless]或no[Modal]
dialogHide { yes | no | 1 | 0 | on | off } 在打印或者打印預覽時對話框是否隱藏,默認爲no
scroll { yes | no | 1 | 0 | on | off } 指明對話框是否顯示滾動條,默認爲yes
edge { sunken | raised } 指明對話框的邊框樣式,默認爲raised
unadorned { yes | no | 1 | 0 | on | off } 默認爲no
注意:dialogHide,edge,unadorned這三個屬性是用在HTA(HTML Aplication)中的,一般網頁上用不到。
    5、參數傳遞 通過vArguments來傳遞參數,類型不限制,對於字符串類型,最大爲4096個字符,也可以傳遞對象,例如:
parent.htm <script> window.showModalDialog("sun.htm","傳遞進去的參數","help:no;scroll:no"); </script> sun.htm <script> alert("傳來的參數:" + window.dialogArguments); </script>
    6、返回值 通過window.returnValue向打開對話框的窗口返回信息,也可以是對象。例如:
parent.htm <script> result=window.showModalDialog("son.htm","","help:no;scroll:no"); alert(result); </script> son.htm <script> window.returnValue="這裏存放返回的結果"; </script>
    7、防止在模態窗口中提交後新開一窗口    在頁面的 <body>前加入<base target="_self">     8、調用父窗口的方法同時傳遞參數
parent.htm <script> function show(){//父窗口的方法 alert("show"); } var arg=new Object();//傳遞進去的參數 arg.win=window;//把當前窗口的引用當參數傳進去 arg.str="argument";//要傳進去的其他參數 window.showModalDialog("son.htm",arg,'help:no'); </script> son.htm <script> var arg=window.dialogArguments; alert(arg.str); arg.win.show();//調用父窗口的方法 </script>

-----------------------------------------------------------------------------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script language="javascript" type="text/javascript">
function opendialog()
{
 window.showModalDialog("http://www.sosuo8.com","","dialogWidth=300px;dialogHeight=200px;status=no;help=no;scroll=no")
}
</script>
<title>無標題文檔</title>
</head>

<body>
<button οnclick="opendialog();">打開</button>
</body>
</html>

-----------------------------------------------------------------------------------------------------------------------------------------------------

傳入參數:
要想對話框傳遞參數,是通過vArguments來進行傳遞的。類型不限制,對於字符串類型,最大爲4096個字符。也可以傳遞對象,例如:

test1.htm
====================

<script>
var mxh1 = new Array("mxh","net_lover","孟子E章")
var mxh2 = window.open("about:blank","window_mxh")
// 向對話框傳遞數組
window.showModalDialog("test2.htm",mxh1)
// 向對話框傳遞window對象
window.showModalDialog("test3.htm",mxh2)
</script>


test2.htm
====================

<script>
var a = window.dialogArguments
alert("您傳遞的參數爲:" + a)
</script>


test3.htm
====================

<script>
var a = window.dialogArguments
alert("您傳遞的參數爲window對象,名稱:" + a.name)
</script>


可以通過window.returnValue向打開對話框的窗口返回信息,當然也可以是對象。例如:

test4.htm
===================

<script>
var a = window.showModalDialog("test5.htm")
for(i=0;i<a.length;i++) alert(a[i])
</script>


test5.htm
===================

<script>
function sendTo()
{
var a=new Array("a","b")
window.returnValue = a
window.close()
}
</script>
<body>
<form>
<input value="返回" type=button οnclick="sendTo()">
</form>


常見問題:
1,如何在模態對話框中進行提交而不新開窗口?
如果你 的 瀏覽器是IE5.5+,可以在對話框中使用帶name屬性的iframe,提交時可以制定target爲該iframe的name。對於IE4+,你可以用高度爲0的frame來作:例子,

test6.htm
===================

<script>
window.showModalDialog("test7.htm")
</script>


test7.htm
===================

if(window.location.search) alert(window.location.search)
<frameset rows="0,*">
<frame src="about:blank">
<frame src="test8.htm">
</frameset>


test8.htm
===================

<form target="_self" method="get">
<input name=txt value="test">
<input type=submit>
</form>
<script>
if(window.location.search) alert(window.location.search)
</script>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章