- 在web开发中,经常会遇到需要弹出一个子窗口的情况,并且子窗口和父窗口之间需要保持良好的通信。如弹出一个子查询窗口,并把查询出的某项信息填充到父窗口的表单中,这时使用window对象的showModalDialog打开一个模态对话框来实现这一功能是非常有用的。
下面是一个简单示例
// src.html
<script type="text/javascript">
window.showModalDialog("des.html");
</script>
// des.html
<script type="text/javascript">
alert("this is des.html");
</script>
2. 为使打开的模态对话框的外观满足我们的要求,必须要设置对话框的样式。该方法的第三个参数可以用来设置我们需要的外观样式。
<script type="text/javascript">
window.showModalDialog("des.html", "", "dialogWidth: 600px; dialogHeight: 600px");
</script>
样式属性的设置是键值对的形式进行的,多个属性设置之间用分号“;”隔开。
可以设置的样式属性如下:
- dialogHeight :对话框高度,不小于100pxpx,IE4中dialogHeight 和 dialogWidth 默认的单位是em,而IE5中是px,为方便其见,在定义modal方式的对话框时,用px做单位。
- dialogWidth: 对话框宽度。
- 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+]:是否显示状态栏。默认为yes[ Modeless]或no[Modal]。
- scroll:{ yes | no | 1 | 0 | on | off }:指明对话框是否显示滚动条。默认为yes。
下面几个属性是用在HTA中的,在一般的网页中一般不使用。
- dialogHide:{ yes | no | 1 | 0 | on | off }:在打印或者打印预览时对话框是否隐藏。默认为no。
- edge:{ sunken | raised }:指明对话框的边框样式。默认为raised。
- unadorned:{ yes | no | 1 | 0 | on | off }:默认为no。
3. 但是最重要的在于如何向模态对话框中传递参数了,相信很多朋友都发现在上面的例子中showModalDialog方法的第二个参数为空字符串,其实我们就是通过它把信息传递到模态对话框中的。
// src.html
<script type="text/javascript">
window.showModalDialog("des.html", "my name is tuozixuan", "dialogWidth: 600px; dialogHeight: 600px");
</script>
在模态对话框窗体中需要用到window对象的dialogArguments属性来获取传递过来的参数(中英文字符都可)。
// des.html
<script type="text/javascript">
var msg = window.dialogArguments
alert(msg);
</script>
在向模态对话框传递参数时,不仅可以是字符串,还可以是Number、数组、对象等类型。
// src.html
<script type="text/javascript">
window.showModalDialog("des.html", 1, "dialogWidth: 600px; dialogHeight: 600px");
</script>
// des.html
<script type="text/javascript">
var msg = window.dialogArguments
alert(msg);
</script>
// src.html
<script type="text/javascript">
window.showModalDialog("des.html", [1,2,3], "dialogWidth: 600px; dialogHeight: 600px");
</script>
// des.html
<script type="text/javascript">
var msg = window.dialogArguments
alert(msg[2]);
</script>
// src.html
<script type="text/javascript">
var obj = new Object();
obj.name = "tuozixuan";
obj.age = 1000;
window.showModalDialog("des.html", obj, "dialogWidth: 600px; dialogHeight: 600px");
</script>
// des.html
<script type="text/javascript">
var msgObj = window.dialogArguments;
alert(msgObj.name + " " + msgObj.age);
</script>
4. 模态对话框向父窗口传递信息
如果需要把模态对话框中的数据传递给父窗口中,需要用到模态对话框中window对象的returnValue属性。当用户关闭模态对话框时,浏览器将会把returnValue属性的值返回给父窗口,若该属性未设置,则返回undefined。
// src.html
<script type="text/javascript">
var obj = new Object();
obj.name = "tuozixuan";
obj.age = 1000;
var resultData = window.showModalDialog("des.html", obj, "dialogWidth: 600px; dialogHeight: 600px");
alert(resultData);
</script>
// des.html
<script type="text/javascript">
window.returnValue = "hello";
</script>
其中window对象的returnValue属性可以设置为数字、字符串、数字、对象等数据类型,情况和从父窗口向模态对话框中传递数据类似,在此不再赘述。
5. 模态对话框调用父窗口的js方法
可以把父窗口的window对象作为参数传递给模态对话框,模态对话框获取到参数对象的句柄后就可以调用父窗口的js方法了。
// src.html
<script type="text/javascript">
window.showModalDialog("des.html", window, "dialogWidth: 600px; dialogHeight: 600px");
function showMsg()
{
alert("飞雪连天射白鹿,笑书神侠倚碧鸳");
}
</script>
// des.html
<script type="text/javascript">
window.dialogArguments.showMsg();
</script>
尾言:由于个人水平有限,如发现任何错误之处,烦请指正,鄙人不胜感激!
分享到:
相关推荐
主要介绍showModalDialog的用法和参数的传递
javascript代码模块之\Window.ShowModalDialog使用手册
window.showModalDialog的基本用法
项目中用到showModalDialog,总结一下使用心得与注意事项
showmodaldialog的用法收集,很詳細的。很好的東東,于大家一起分享
window.showModalDialog以及window.open用法简介
Selenium目前没有提供对IE模态对话框(即通过showModalDialog方法打开的弹出对话框)的处理。原因在于,模态对话框会将父页面的 JS挂起,直至对话框处理完毕才会继续执行父页面JS。因为Selenium的底层实现是基于JS的,...
div层实现showModalDialog
window.showModalDialog以及window.open用法简介
showModalDialog参数传递和获
This is a `window.showModalDialog()` shim using a modal HTML5 `<dialog>` element and ECMAScript 6 Generators. It was tested in the latest Google Chrome with the *Enable Experimental JavaScript* flag ...
showModalDialog和showModelessDialog使用心得
ShowModalDialog函数的功能: 使用方法: 参数说明:
javascript代码模块之window.showModalDialog方法的使用
showModalDialog参数详解 基本介绍: showModalDialog() (IE 4+ ...window.showModalDialog()方法用来创建一个显示HTML内容的模态对话框。 window.showModelessDialog()方法用来创建一个显示HTML内容的非模态对话框。
showModalDialog和showModelessDialog的用法,彼此之间有什么不同? 用事例进行说明。 如:对话框赋给父页面的值 步骤一:父页面CS文件Page_load中执行脚本(打开对话框) Button1.Attributes.Add("onclick","window...
解决三层或心上showModalDialog的问题_2
showModalDialog刷新窗口的例子