showModalDialog模态对话框的使用详解以及浏览器兼容 |
|||||||||||||||||||||||||||||||||||||||||||
本文标签:showModalDialog 1.ModalDialog是什么? 2.一个例子 main.html 复制代码 代码如下: <HTML> <HEAD> <METANAME="GENERATOR"Content="oscar999"> </HEAD> <script> functionshowmodal() { varret=window.showModalDialog("sub.html?temp="+Math.random()); alert("subreturnvalueis"+ret); } </script> <BODY> <INPUTid=button1type=buttonvalue="opensub"name=button1onclick="showmodal();"> </BODY> </HTML> sub.html 复制代码 代码如下: <HTML> <HEAD> <METANAME="GENERATOR"Content="oscar999"> </HEAD> <script> functionreturnMain() { window.returnValue="returnfromsub"; window.close(); } </script> <BODY> <INPUTid=button1type=buttonvalue="returnandclose"name=button1onclick="returnMain()"> </BODY> </HTML> 特别说明:在main.html中showModalDialog的方法时,有使用到Math.random()的目的是避免缓存 。 3.showModalDialog详细使用 还有几个属性是用在HTA中的,在一般的网页中一般不使用 。 4.浏览器兼容
如果有传入vArguments这个参数为window的话: 复制代码 代码如下: var ret = window.showModalDialog("sub.html?temp="+Math.random(),window); 则在子窗口中,以下的值为:
注意一下Firefox浏览器下,子窗体假如刷新的话window.dialogArguments照样会丢失,变成undefined 。以上结果中我们可以看出返回值returnValue就只有chrome浏览器返回的是undefined,其他浏览器都没有问题 5.如何解决Chrome的兼容问题 。 复制代码 代码如下: <HTML> <HEAD> <META NAME="GENERATOR" Content="oscar999"> </HEAD> <script> function showmodal() { var ret = window.showModalDialog("sub.html?temp="+Math.random(),window); //for Chrome if(ret==undefined) { ret = window.returnValue; } alert("sub return value is "+ret); } </script> <BODY> <INPUT id=button1 type=button value="open sub" name=button1 onclick="showmodal();"> </BODY> </HTML> sub.html 复制代码 代码如下: <HTML> <HEAD> <META NAME="GENERATOR" Content="oscar999"> </HEAD> <script> function returnMain() { if(window.opener!=undefined) { window.opener.returnValue = "return from sub"; }else{ window.returnValue = "return from sub"; } window.close(); } </script> <BODY> <INPUT id=button1 type=button value="return and close" name=button1 onclick="returnMain()"> </BODY> </HTML> 这里是判断某些对象是否为defined来区分浏览器 。当然,也可以判断浏览器的类型的方式进行 这里是借用了父窗口的returnValue来使用, 如果父窗口的returnValue也用其他用途是可以使用替换的方式进行了, as: 6.需要特别注意的是,Chrome下的测试需要把html 文件放入到web server(Tomcat,...)下通过http url 访问测试 。否则就不成功了 。 |