JS對話框_JS模態(tài)對話框showModalDialog用法總結(jié)
父窗口:
<html>
<head>
<title>無標(biāo)題頁</title>
<script language="javascript" type="text/javascript">
function opendialog1()
{
var someValue=window.showModalDialog("b.html","","dialogWidth=500px;dialogHeight=500px;status=no;help=no;scrollbars=no");
document.form1.p1t.value=someValue;
}
</script>
</head>
<body>
<form name="form1" action="#">
<input type="text" name="p1t">
<input type="button" value="打開對話框" onclick="opendialog1()">
</form>
</body>
</html>
子窗口:
<html>
<head>
<title>無標(biāo)題頁</title>
<script language="javascript" type="text/javascript">
function a()
{
var wname = document.form1.backname.value;
parent.window.returnValue=wname; //父窗口就是上一個頁面
window.close();
}
</script>
</head>
<body>
<form name="form1" action="">
<input type="text" value="輸入要傳的值" name="backname">
<input type="button" value="傳值" onclick="return a()">
</form>
</body>
</html>
---- 1. 基本語法
---- window對象有一個方法稱為showModalDialog ,我們可以在頁面按鈕的onclick中寫如下代碼:
< BUTTON onclick="window.showModalDialog
('dialog.htm')" >Search< /BUTTON >
---- 系統(tǒng)會在新窗口中打開dialog.htm頁面,并且等待用戶響應(yīng),如果用戶不響應(yīng)該頁面,那么主頁面將得不到光標(biāo)。
---- 在dialog.htm中設(shè)置window對象的returnValue屬性,就可以讓主頁面得到返回值。例如,在頁面的確定按鈕的onclick中寫:
window.returnValue = window.
document.all.iptPeopleID.value
---- 將輸入框iptPeopleID的值賦給window對象的returnValue屬性。在主頁面中就可以得到這個值:
var str = showModalDialog ("dialog1.htm")
---- 2. 傳遞初始化值
---- 如果要向模態(tài)頁面中傳遞初始化值,可以在主頁面中用如下語句:
var cSearchValue=showModalDialog
('dialog.htm', 'ABC')
在dialog.htm中使用window.dialogArguments
屬性可以得到'ABC'。例如:
< SCRIPT FOR=window EVENT=onload LANGUAGE="JScript" >
if (window.dialogArguments != null)
window.document.all.iptPeopleID.value = window.dialogArguments;
< /SCRIPT >
---- 3. 傳遞多個數(shù)值
---- 如果要向模態(tài)頁面中傳遞多個參數(shù),可以先在主頁面中定義一個對象,
function myDialog() {
var str1;
var str2;
}
---- 顯示模態(tài)頁面之前初始化該對象,然后調(diào)用showModalDialog 方法。
function WelcomeYou(iniStr1,iniStr2) {
myDialog.str1 = iniStr1;
myDialog.str2 =iniStr2 ;
if (showModalDialog ("dialog2.htm", myDialog)
==false) //將對象傳入
......
---- 在模態(tài)頁面中,可以用這樣的代碼
window.document.all.iptID.value=
window.dialogArguments.str1
來引用數(shù)值,或者用這樣的代碼對之賦值
window.dialogArguments.str1 =
window.document.all.iptID.value
---- 完整例程如下:
---- 對于只傳遞一個參數(shù)的情況,見:Main1.htm和dialog1.htm。
---- 對于傳遞多個參數(shù)的情況,見:Main2.htm和dialog2.htm。
父窗體:
dim xxx '返回值
dim yyy '傳到子窗體的參數(shù)
var xxx = ShowModalDialog ('xxx.asp','yyy','dialogWidth:100px;DialogHeight=290px;status:no')
子窗體:
dim yyy '從父窗體傳來的參數(shù)
dim xxx '傳回父窗體的參數(shù)
yyy = window.dialogArguments
xxx = window.returnValue
window.dialogArguments的用法
Javascript有許多內(nèi)建的方法來產(chǎn)生對話框,如:window.alert(),window.confirm(),window.prompt().等。然而IE提供更多的方法支持對話框。如:
showModalDialog()(IE4+支持)
showModelessDialog()(IE5+支持)
window.showModalDialog()方法用來創(chuàng)建一個顯示HTML內(nèi)容的模態(tài)對話框,由于是對話框,因此它并沒有一般用window.open()打開的窗口的所有屬性。
window.showModelessDialog()方法用來創(chuàng)建一個顯示HTML內(nèi)容的非模態(tài)對話框。
當(dāng)我們用showModelessDialog()打開窗口時,不必用window.close()去關(guān)閉它,當(dāng)以非模態(tài)方式[IE5]打開時,打開對話框的窗口仍可以進行其他的操作,即對話框不總是最上面的焦點,當(dāng)打開它的窗口URL改變時,它自動關(guān)閉。而模態(tài)[IE4]方式的對話框始終有焦點(焦點不可移走,直到它關(guān)閉)。模態(tài)對話框和打開它的窗口相聯(lián)系,因此我們打開另外的窗口時,他們的鏈接關(guān)系依然保存,并且隱藏在活動窗口的下面。
使用方法如下:
vReturnValue=window.showModalDialog(sURL[,vArguments][,sFeatures])
vReturnValue=window.showModelessDialog(sURL[,vArguments][,sFeatures])
參數(shù)說明:
sURL
必選參數(shù),類型:字符串。用來指定對話框要顯示的文檔的URL。
vArguments
可選參數(shù),類型:變體。用來向?qū)υ捒騻鬟f參數(shù)。傳遞的參數(shù)類型不限,包括數(shù)組等。對話框通過window.dialogArguments來取得傳遞進來的參數(shù)。
sFeatures
可選參數(shù),類型:字符串。用來描述對話框的外觀等信息,可以使用以下的一個或幾個,用分號“;”隔開。
dialogHeight對話框高度,不小于100px,IE4中dialogHeight和dialogWidth默認的單位是em,而IE5中是px,為方便其見,在定義modal方式的對話框時,用px做單位。
dialogWidth:對話框?qū)挾取?BR>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+]:是否顯示狀態(tài)欄。默認為yes[Modeless]或no[Modal]。
scroll:{yes|no|1|0|on|off}:指明對話框是否顯示滾動條。默認為yes。
還有幾個屬性是用在HTA中的,在一般的網(wǎng)頁中一般不使用。
dialogHide:{yes|no|1|0|on|off}:在打印或者打印預(yù)覽時對話框是否隱藏。默認為no。
edge:{sunken|raised}:指明對話框的邊框樣式。默認為raised。
unadorned:{yes|no|1|0|on|off}:默認為no。
傳入?yún)?shù):
要想對話框傳遞參數(shù),是通過vArguments來進行傳遞的。類型不限制,對于字符串類型,最大為4096個字符。也可以傳遞對象,例如:
test1.htm
====================
<script>
varmxh1=newArray("mxh","net_lover","孟子E章")
varmxh2=window.open("about:blank","window_mxh")
//向?qū)υ捒騻鬟f數(shù)組
window.showModalDialog("test2.htm",mxh1)
//向?qū)υ捒騻鬟fwindow對象
window.showModalDialog("test3.htm",mxh2)
</script>
test2.htm
====================
<script>
vara=window.dialogArguments
alert("您傳遞的參數(shù)為:"+a)
</script>
test3.htm
====================
<script>
vara=window.dialogArguments
alert("您傳遞的參數(shù)為window對象,名稱:"+a.name)
</script>
可以通過window.returnValue向打開對話框的窗口返回信息,當(dāng)然也可以是對象。例如:
test4.htm
===================
<script>
vara=window.showModalDialog("test5.htm")
for(i=0;i<a.length;i++)alert(a[i])
</script>
test5.htm
===================
<script>
functionsendTo()
{
vara=newArray("a","b")
window.returnValue=a
window.close()
}
</script>
<body>
<form>
<inputvalue="返回"type=buttononclick="sendTo()">
</form>
常見問題:
1,如何在模態(tài)對話框中進行提交而不新開窗口?
如果你的瀏覽器是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)
<framesetrows="0,*">
<framesrc="about:blank">
<framesrc="test8.htm">
</frameset>
test8.htm
===================
<formtarget="_self"method="get">
<inputname=txtvalue="test">
<inputtype=submit>
</form>
<script>
if(window.location.search)alert(window.location.search)
</script>
2,可以通過http://servername/virtualdirname/test.htm?name=mxh方式直接向?qū)υ捒騻鬟f參數(shù)嗎?
答案是不能。但在frame里是可以的。
- JS 模態(tài)對話框和非模態(tài)對話框操作技巧匯總
- 利用javascript打開模態(tài)對話框(示例代碼)
- javascript showModalDialog模態(tài)對話框使用說明
- JavaScript 實現(xiàn)模態(tài)對話框 源代碼大全
- 詳解AngularJS 模態(tài)對話框
- 兩種WEB下的模態(tài)對話框 (asp.net或js的分別實現(xiàn))
- js模態(tài)對話框使用方法詳解
- js實現(xiàn)div模擬模態(tài)對話框展現(xiàn)URL內(nèi)容
- ModelDialog JavaScript模態(tài)對話框類代碼
- JavaScript實現(xiàn)模態(tài)對話框?qū)嵗?/a>
- js實現(xiàn)響應(yīng)按鈕點擊彈出可拖拽的非模態(tài)對話框完整實例【測試可用】
相關(guān)文章
javascript下利用arguments實現(xiàn)string.format函數(shù)
sitepoint上看到Andrew Tetlaw在08年寫的文章arguments: A JavaScript Oddity,閱讀之后,除了對arguments溫故知新一遍以外,印象最深刻的還是Andrew的第一個函數(shù)實現(xiàn)的string.format功能。2010-08-08關(guān)于onScroll事件在IE6下每次滾動觸發(fā)三次bug說明
今天測試發(fā)現(xiàn)IE6下用window.onscroll,每次滾動時會觸發(fā)3次,而火狐、IE7沒此問題,應(yīng)該是IE6的一個BUG2011-09-09讓iframe子窗體取父窗體地址欄參數(shù)(querystring)
突然用到,記錄一下,對地址欄字符串用正則處理最好,有時間研究一下。 主要是思路。2009-10-10javascript下拉列表中顯示樹形菜單的實現(xiàn)方法
這篇文章主要介紹了javascript下拉列表中顯示樹形菜單的實現(xiàn)方法,需要的朋友可以參考下2015-11-11JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹插入節(jié)點、生成二叉樹示例
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹插入節(jié)點、生成二叉樹,結(jié)合實例形式較為詳細的分析了javascript二叉樹相關(guān)概念、定義、節(jié)點插入、遍歷輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02