开发中少不了的Fun -- 微信开发IOS端alert/confirm提示信息,去除网址(URL)的方法

在微信公众号开发的时候在使用【alert/confirm】弹出提示或者警告信息的时候,【alert/confirm】会将该公众号的网址显示出来,这样很不美观。所以很多时候我们会选择去除那个网址提示内容。解决方法如下:

重写alert

    //微信浏览器中,aler弹框不显示域名
    //先判断是否为微信浏览器
     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

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

特别感谢:内容摘自 屌丝爷霉儿

原文地址:https://www.cnblogs.com/lisaShare/p/10655526.html