替换window.showModalDialog 已解决!!!

记录一下
最近在维护一个项目,使用IE一切正常,但是切谷歌的话 正常登陆都不行了。
检查了一下 发现报错。
在这里插入图片描述
后来查了一下 发现 window.showModalDialog 方法在很久以前就被弃用了!!!

前期解决方案:用window.open来替代。(有bug 别用!!!

// 定义一个全局变量判定是否原生支持showModalDialog方法
var has_showModalDialog = !!window.showModalDialog;
if(!has_showModalDialog){
    
    
	//定义window.showModalDialog如果它不存在
	if(window.showModalDialog == undefined){
    
    
		window.showModalDialog =  window.open(url, "_blank", 
			"width="+screen.availWidth+",height="+screen.availHeight+",scroll=0");
	}
}

把上面这段代码直接加到使用window.showModalDialog之前就好了。
上面这个用后会出现一个问题:由于原有页面未关闭,当从子页面返回后会报错,需刷新后可用。

后期解决方案:用js判断当前使用的浏览器是不是IE (无bug可用!!!)

if(IEVersion() == -1 || IEVersion() == "edge"){
    
    
	window.showModalDialog =  window.open(url, "_blank", 
		"width="+screen.availWidth+",height="+screen.availHeight+",scroll=0,");
}

function IEVersion() {
    
    
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    if(isIE) {
    
    
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if(fIEVersion == 7) {
    
    
            return 7;
        } else if(fIEVersion == 8) {
    
    
            return 8;
        } else if(fIEVersion == 9) {
    
    
            return 9;
        } else if(fIEVersion == 10) {
    
    
            return 10;
        } else {
    
    
            return 6;//IE版本<=7
        }   
    } else if(isEdge) {
    
    
        return 'edge';//edge
    } else if(isIE11) {
    
    
        return 11; //IE11  
    }else{
    
    
        return -1;//不是ie浏览器
    }
}

还发现一个问题:使用window.open 打开子页面时,头部的地址栏无法隐藏!!!

关于window.open的详解:https://www.w3cschool.cn/jsref/met-win-open.html
判断是否是IE浏览器:https://www.cnblogs.com/liuyanxia/p/5855760.html

OK 结束了!!!

猜你喜欢

转载自blog.csdn.net/c15162/article/details/118933612
今日推荐