Detect if an alert or confirm is displayed on a page

前端 未结 6 1790
傲寒
傲寒 2020-11-29 06:34

Is there a way using JavaScript or jQuery to detect if a confirm or alert box is being displayed?

6条回答
  •  一向
    一向 (楼主)
    2020-11-29 07:21

    If you want to detect if these are being blocked. You will have to do your own thing with the message you will be dispalying but override the native alert/confirm.

    window.nativeAlert = window.alert;
    window.alert = function (message) {
    var timeBefore = new Date();
    var confirmBool = nativeAlert(message);
    var timeAfter = new Date();
    if ((timeAfter - timeBefore) < 350) {
        MySpecialDialog("You have alerts turned off, turn them back on or die!!!");
      }
    }
    
    window.nativeConfirm = window.confirm;
    window.confirm = function (message) {
    var timeBefore = new Date();
    var confirmBool = nativeConfirm(message);
    var timeAfter = new Date();
    if ((timeAfter - timeBefore) < 350) {
        MySpecialDialog("You have alerts turned off, turn them back on or die!!!");
    }
     return confirmBool;
    }
    

    Obviously I have set the time to 3.5 milliseconds. But after some testing we were only able to click or close the dialogs in about 5 milliseconds plus

提交回复
热议问题