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

原生的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等

猜你喜欢

转载自blog.csdn.net/weixin_41635750/article/details/108020938