基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog() 方法用来创建一个显示HTML内容的模态对话框。 window.showModelessDialog() 方法用来创建一个显示HTML内容的非模态对话框。 使用方法: vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures]) vReturnValue = window.showModelessDialog(sURL [, vArguments] [,sFeatures]) 参数说明: sURL -- 必选参数,类型:字符串。用来指定对话框要显示的文档的URL。 vArguments -- 可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括 数组等。 对话框通过window.dialogArguments来取得传递进来的参数。 sFeatures -- 可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的 一个或几个,用分号“;”隔开。 ---------------- 1. dialogHeight: 对话框高度,不小于100px 2. dialogWidth: 对话框宽度。 3. dialogLeft: 离屏幕左的距离。 4. dialogTop: 离屏幕上的距离。 5. center: { yes | no | 1 | 0 } : 是否居中,默认yes,但仍可以指定高度和宽度。 6. help: {yes | no | 1 | 0 }: 是否显示帮助按钮,默认yes。 7. resizable: {yes | no | 1 | 0 } [IE5+]: 是否可被改变大小。默认no。 8. status: {yes | no | 1 | 0 } [IE5+]: 是否显示状态栏。默认为yes[ Modeless]或no[Modal]。 9. scroll: { yes | no | 1 | 0 | on | off }:是否显示滚动条。默认为yes。 下面几个属性是用在HTA中的,在一般的网页中一般不使用。 10. dialogHide:{ yes | no | 1 | 0 | on | off }:在打印或者打印预览时对话框是否隐藏。默认为no。 11. edge:{ sunken | raised }:指明对话框的边框样式。默认为raised。 12. unadorned:{ yes | no | 1 | 0 | on | off }:默认为no。 参数传递: 1. 要想对话框传递参数,是通过vArguments来进行传递的。类型不限制,对于字符串类型,最大 为4096个字符。也可以传递对象,例如: ------------------------------- parent.htm <script> var obj = new Object(); obj.name="51js"; window.showModalDialog("modal.htm",obj,"dialogWidth=200px;dialogHeight=100px"); </script> modal.htm <script> var obj = window.dialogArguments alert("您传递的参数为:" + obj.name) </script> ------------------------------- 2. 可以通过window.returnValue向打开对话框的窗口返回信息,当然也可以是对象。例如: ------------------------------ parent.htm <script> str =window.showModalDialog("modal.htm",,"dialogWidth=200px;dialogHeight=100px"); alert(str); </script> modal.htm <script> window.returnValue="http://homepage.yesky.com"; </script> 用window.showModalDialog 或者window.showModelessDialog打开一个模式窗口后,和父窗口的一些交互问题。 要进行交互操作的前提,在调用showModalDialog或者showModelessDialog方法的时候,第二个参数传window,如:
- window.showModelessDialog('filename.htm',window,'dialogWidth=200px;dialogHeight=250px;')
接下来,就是取得父窗口的一些数据和方法,这是经常会用的,父窗口取子窗口的参数一般通过returnValue就可以搞定了~
- //取得父窗口的JS变量 var
window.dialogArguments.var; //获得父窗口的对象和属性 window.dialogArguments.form1.name.value ; //调用父窗口的方法 fun window.dialogArguments.fun() ; 例: a.htm: <SCRIPT language="javascript"> <!-- function OpenWin() { var getv = showModalDialog("eg.htm", "egwin", "dialogWidth:420px; dialogHeight:220px;status:no;help:yes"); if (getv != null) { TextInfo.value=getv.split(",")[0];; aa.value=getv.split(",")[1];; } } //--> </SCRIPT> </head> <input type="text" name="TextInfo"> <input type="text" name="aa"> <input type="button" name="Submit" value="打开" onClick="OpenWin()"> eg.htm: <SCRIPT language="javascript"> <!-- function GetValue() { window.returnValue=TextName.value+","+aa.value; window.close(); } //--> </SCRIPT> <input name="TextName" type="text" id="TextName" value="因为有你而精彩"> <input name="aa" type="text" id="aa" value="MKLove"> <input type="button" name="Submit" value="关闭" onClick="GetValue()"> </p> </div> </body> </html> 弹出一个对话框 showModalDialog 打开独占方式网页对话框 window.showModalDialog 打开一个独占方式网页对话框 话法|Syntax variant = object.showModalDialog(sURL [, vArguments [, sFeatures]]) 参数 描述 sURL 指点URL文件地址 vArguments sFeatures 窗口对话框参数 参数包括下面 可选 dialogWidth:number 设置对话框宽度. 可选 dialogHeight:number 设置对话框高度. 可选 dialogTop:number 设置对话窗户的最高的位置放相对桌面的上面的位置 可选 dialogLeft:number 设置对话窗户左边的位置放相对桌面的左边的位置 可选 center:{yes | no | 1 | 0 } 对话窗口出位位置 yes|1居中 NO|0 默认 可选 Help: {yes|no 1|0} 对话框是否出现帮助按钮 可选 scroll: {yes|no 1|0} 对话框是否出现滚动栏 可选 status: {yes|no 1|0} 对话框是否出现状态栏 可选 传入参数: 要想对话框传递参数,是通过vArguments来进行传递的。类型不限制,对于字符串类型,最大为4096个字符。也可以传递对象,例如: test1.htm ==================== <script> var mxh1 = new Array("mxh","net_lover","孟子E章") var mxh2 = window.open("about:blank","window_mxh") // 向对话框传递数组\\r window.showModalDialog("test2.htm",mxh1) // 向对话框传递window对象\\r 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> 网页对窗口控制已经有很多文章介绍了,但控制对话框的技巧却不是很多,下面是一些基本的控制方法: window.showModelessDialog("url","name","参数:值;参数:值;……") url 对话框窗口链接地址 name 对话框的名称,可以为空 scroll 是否有滚动条,0表示无,非0表示有 status 是否有状态栏,0表示无,非0表示有 help 是否有问号,0表示无,非0表示有 resizable 是否可以用鼠标拖动改变框提大小,0表示不可以,非0表示可以 dialogWidth 对话框宽度值 dialogHeight 对话框高度值 window.showModelessDialog("http://xbs.3322.org/","dialogwin", "scroll:0;status:0;help:1;resizable:1;dialogWidth:480px; dialogHeight:320px") 模态窗口(showModalDialog)的专题讨论(资料收集) 讨论内容 模态窗口的打开 模态窗口的关闭 模态窗口的参数传递 其他 模态窗口的打开 window.showModalDialog("DialogPage.aspx","newwin","dialogHeight: 200px; dialogWidth: 150px; dialogTop: 458px; dialogLeft: 166px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;"); 模态窗口的关闭 window.close(); 模态窗口的参数传递 传值 ParentPage.aspx: window.showModalDialog("DialogPage.aspx?para1=aaa¶2=bbb"); DialogPage.aspx: string str1=Request.QueryString["para1"].toString(); string str2=Request.QueryString["para2"].toString(); 返回值 DialogPage.aspx: window.returnValue="aaa"; ParentPage.aspx: var str=window.showModalDialog("DialogPage.aspx"); 其他 aspx页面在showmodeldialog情况下为什么一提交就重新打开一个页面? showmodaldialog打开的页面中在<head></head>之间加入一行:<base target="_self"> 如果是在数据绑定的模式窗体中,还可以在DataGrid中创建一个模板列,再加入Html的按钮,在按钮中加入:OnClick="returnValue='<%#DataBind.Eval(Container.DataItem,"Name")%>';window.close()" 就可以实现在模式对话框中传递DataGrid的具体选中的行的相关值。 <script language="JavaScript"> function ForceWindow () { this.r = document.documentElement; this.f = document.createElement("FORM"); this.f.target = "_blank"; this.f.method = "post"; this.r.insertBefore(this.f, this.r.childNodes[0]); } ForceWindow.prototype.open = function (sUrl) { this.f.action = sUrl; this.f.submit(); } var myWindow = new ForceWindow(); myWindow.open("try1.html"); </script> |