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");
    }

该方法亲测可行。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值