JS打开模态窗口showModalDialog

“人人尽说江南好,游人只合江南老”
今天在使用谷歌浏览器通过JS打开模态窗口时,出现如下错误:

Uncaught TypeError: window.showModalDialog is not a function

这里写图片描述
出现该问题的原因是,谷歌浏览器已经不支持该方法了。
解决方法:

//兼容谷歌浏览器,如果不能直接打开模态窗口,则自定义一个窗口
            if(!window.showModalDialog){
                window.showModalDialog=function(url,name,option){
                    if(window.hasOpenWindow){
                        window.newWindow.focus();
                    }
                    var re = new RegExp(";", "g");
                    var option  = option.replace(re, '","'); //把option转为json字符串
                    var re2 = new RegExp(":", "g");
                    option = '{"'+option.replace(re2, '":"')+'"}';
                    option = JSON.parse(option);
                    var openOption = 'width='+parseInt(option.dialogWidth)+',height='+parseInt(option.dialogHeight)+',left='+(window.screen.width-parseInt(option.dialogWidth))/2+',top='+(window.screen.height-30-parseInt(option.dialogHeight))/2;
                    window.hasOpenWindow = true;
                    window.newWindow = window.open(url,name,openOption);
                }
            }

function showModal(){
        window.showModalDialog("http://www.baidu.com",window,"dialogWidth:500px;dialogHeight:550px");
    }

该方法亲测可行。

猜你喜欢

转载自blog.csdn.net/A_Runner/article/details/82260189