深圳全飞鸿

标题: chrome中showModalDialog解决方案 [打印本页]

作者: zhgc    时间: 2021-4-24 11:16
标题: chrome中showModalDialog解决方案

未测试




调用myShowModalDialog
  1. function myShowModalDialog(url, width, height, callback)
  2. {
  3.     if(window.showModalDialog)
  4.     {
  5.         if(callback)
  6.         {
  7.             var rlt = showModalDialog(url, '', 'resizable:no;scroll:no;status:no;center:yes;help:no;dialogWidth:' + width + ' px;dialogHeight:' + height + ' px');
  8.             if(rlt)
  9.                 return callback(rlt);
  10.             else
  11.             {
  12.                 callback(window.returnValue);
  13.             }
  14.         }
  15.         else
  16.             showModalDialog(url, '', 'resizable:no;scroll:no;status:no;center:yes;help:no;dialogWidth:' + width + ' px;dialogHeight:' + height + ' px');
  17.     }
  18.     else
  19.     {
  20.         if(callback)
  21.             window.showModalDialogCallback = callback;
  22.         var top = (window.screen.availHeight-30-height)/2; //获得窗口的垂直位置;
  23.         var left = (window.screen.availWidth-10-width)/2; //获得窗口的水平位置;
  24.         var winOption = "top="+top+",left="+left+",height="+height+",width="+width+",resizable=no,scrollbars=no,status=no,toolbar=no,location=no,directories=no,menubar=no,help=no";
  25.         window.open(url,window, winOption);
  26.     }
  27. }
复制代码

  


test.html中脚本函数
  1. <script type='text/javascript'>//在点击网页中确定按钮调用SetReturnValue
  2. function SetReturnValue()
  3. {
  4.     var rlt = "我想返回的数值";
  5.     window.returnValue = rlt;//ie之类浏览器
  6.     if(opener != null && opener != 'undefined')
  7.     {
  8.         window.opener.returnValue = rlt; //chrome有些版本有问题, 所以在子窗口同时修改了父窗口的ReturnValue(能执行showModalDialog的chrome)
  9.         if(window.opener.showModalDialogCallback)
  10.             window.opener.showModalDialogCallback(rlt);
  11.     }
  12.     window.close();
  13. }
  14. </script>
复制代码

  

调用方式
  1. myShowModalDialog("http://test.com/test.html", 500, 4000, function(resv){
  2.          alert(resv);//原窗口关闭处理流程
  3. });
复制代码

  




欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2