Javascript 检测页面上是否显示警报或确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4866986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:28:59  来源:igfitidea点击:

Detect if an alert or confirm is displayed on a page

javascriptjqueryalert

提问by Uggers2k

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

有没有办法使用 JavaScript 或 jQuery 来检测是否正在显示确认或警告框?

回答by user113716

If you wanted to run some code when an alert()fires, you could try something like this:

如果你想在alert()触发时运行一些代码,你可以尝试这样的事情:

I've only tested in Chrome, so I'm not sure about browser support.

我只在 Chrome 中测试过,所以我不确定浏览器支持。

Example:http://jsfiddle.net/Q785x/1/

示例:http : //jsfiddle.net/Q785x/1/

(function() {
    var _old_alert = window.alert;
    window.alert = function() {
                     // run some code when the alert pops up
        document.body.innerHTML += "<br>alerting";
        _old_alert.apply(window,arguments);
                     // run some code after the alert
        document.body.innerHTML += "<br>done alerting<br>";
    };
})();

alert('hey');
alert('you');
alert('there');

Of course this only lets you run code before and after an alert. As @kander noted, javascript execution is halted while the alert is displayed.

当然,这只允许您在警报之前和之后运行代码。正如@kander 所指出的,在显示警报时 javascript 执行会停止。

回答by Raynos

No there is not. You can check that the return value of a confirmcommand is indeed trueor falsebut you cant check whether there visually there.

不,那里没有。您可以检查confirm命令的返回值是否确实存在truefalse但无法直观地检查是否存在。

These things are part of the browser not part of the DOM. I'm sure there's a dirty hack that works for IE because it's a bastardized child of the windows OS.

这些东西是浏览器的一部分,而不是 DOM 的一部分。我确信有一个适用于 IE 的肮脏黑客,因为它是 Windows 操作系统的一个混蛋孩子。

回答by Dan Beam

You could do this if you want to...

如果你愿意,你可以这样做...

(function () {

    // remember the normal alert
    var oldAlert = (function(){ return this.alert; }()),
        oldConfirm = (function(){ return this.confirm; }());

    // inject ourself into the window.alert and window.confirm globals
    alert = function (msg) {
        oldAlert.call(document, msg);
        document.onAlert(msg);
    };
    confirm = function (msg) {
        var result = oldConfirm.call(document, msg);
        document.onConfirm(msg, result);
        return result;
    };

    // these just chill and listen for events
    document.onAlert = function (msg) {
        window.console && console.log('someone alerted: ' + msg);
    };
    document.onConfirm = function (msg) {
        window.console && console.log('someone was asked: ' + msg);
        window.console && console.log('and they answered: ' + (msg ? 'yes' : 'no'));
    };

}());

The downside to this is that

这样做的缺点是

回答by DeadlyChambers

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

显然我已将时间设置为 3.5 毫秒。但是经过一些测试,我们只能在大约 5 毫秒内点击或关闭对话框

回答by Andrii Karaivanskyi

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).

要添加到@ user113716 的答案中,您可以依靠时间。我假设如果确认对话框花费的时间少于 200 毫秒,它将被浏览器阻止。如果确认对话框被阻止,我在下面返回 true(默认情况下它返回 false,代码在 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;
    }

回答by kander

Confirm and alert boxes are blocking events - Javascript code execution is halted while these are displayed. So no - you can not detect if one is currently being displayed, as far as I know.

确认框和警报框正在阻止事件 - 显示这些时 Javascript 代码执行停止。所以不 - 据我所知,您无法检测当前是否正在显示一个。