window.alert()暴露网址问题

211 阅读1分钟

在部分项目测试中发现,用微信浏览器跑项目,window.alert()方法提示框的title会暴露url地址,很不美观

找到了如下解决方法(重写alert方法)

window.alert = function(name:string){
  let 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);
}