微信浏览器中,aler弹框不显示域名

开发微信页面过程中,出于安全与美观等原因,我们需要重写页面的alert,confirm方法:

//微信浏览器中,aler弹框不显示域名
    (function(){
        //先判断是否为微信浏览器
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            //重写alert方法,alert()方法重写,不能传多余参数
            window.alert = function(name){
                var iframe = document.createElement("IFRAME");
                iframe.style.display="none";
                iframe.setAttribute("src", 'data:text/plain');
                document.documentElement.appendChild(iframe);
                window.frames[0].window.alert(name);
                iframe.parentNode.removeChild(iframe);
            }
        }
    })();

     //微信浏览器中,confirm弹框不显示域名
    (function(){
        //先判断是否为微信浏览器
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            //重写confirm方法,confirm()方法重写,不能传多余参数
            window.confirm = function (message) {
               var iframe = document.createElement("IFRAME");
               iframe.style.display = "none";
               iframe.setAttribute("src", 'data:text/plain,');
               document.documentElement.appendChild(iframe);
               var alertFrame = window.frames[0];
               var result = alertFrame.window.confirm(message);
               iframe.parentNode.removeChild(iframe);
               return result;
            };
        }
    })();

猜你喜欢

转载自blog.csdn.net/qq_38194124/article/details/82017926