Detect if an alert or confirm is displayed on a page

前端 未结 6 1800
傲寒
傲寒 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:20

    To add to @user113716's answer you can rely on time. I assume that if confirm dialog took less than 200 ms, it is blocked by browser. Below I return true if confirm dialog is blocked (by default it returns false, the code is in TypeScript).

        let oldConfirm = window.confirm;
        window.confirm = (msg) => {
            let time = new Date().getTime();
            let conf = oldConfirm(msg);
    
            return new Date().getTime() - time > 200 ? conf : true;
        }
    

提交回复
热议问题