当前位置: 代码迷 >> 综合 >> js清除或隐藏alert和confirm中的网址内容
  详细解决方案

js清除或隐藏alert和confirm中的网址内容

热度:65   发布时间:2024-02-11 09:15:16.0

原生的alert和confirm都会带有网址内容,如要清除,需要重写这两个函数

  • 重写alert

window.alert = function (message) {var iframe = document.createElement("IFRAME");iframe.style.display = "none";document.documentElement.appendChild(iframe);window.frames[0].window.alert(message);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;
}
  • 如果非强制使用原声弹窗,可以使用第三方的UI弹窗库替代,比如layer,mui等

  相关解决方案