【chrome&Edge不兼容openModalDialog弹出模态窗口window.showModalDialog解决办法】

chrome和edge不兼容openModalDialog弹出模态窗口解决办法

实现chrome/Edge/IE 都支持弹窗,现在项目中只能在IE打开弹窗页面,在chrome/Edge无法打开弹窗。

解决办法分两步:

  1. 设置谷歌支持弹出窗口和重定向
    1.1 打开谷歌浏览器==》设置==》安全和隐私性==》弹出式窗口和重定向==》设置允许
  2. 代码设置判断不同内核不同打开方式。
    2.1 如果是chrome内核,那么用window.open打开窗口。 否则使用window.showModalDialog。

代码片段如下:

if (navigator.userAgent.indexOf("Chrome") > 0) {
        // alert("openModalDialogs open is forbidded!");
        var paramsChrome = 'height=' + height + ', width=' + width + ', top=' + (((window.screen.height - height) / 2) - 50) +
            ',left=' + ((window.screen.width - width) / 2) + ',toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no';
        window.open(url, "openModalDialogs", paramsChrome);
    }else {
        // alert("openModalDialogs open success!");
        var returnValue = window.showModalDialog(url,parameter,"dialogWidth:"+width+";dialogHeight:"+height+";status:no;help:no;edge:sunken;toolbar=yes;menubar=yes;location=yes;scroll:yes;resizable:yes");
        return returnValue;
    }

亲测有效!有问题评论区见!


版权声明:本文为weixin_50188421原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。