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

Javascript 检测页面上是否显示警报或确认,javascript,jquery,alert,Javascript,Jquery,Alert,有没有一种方法可以使用JavaScript或jQuery来检测是否显示确认框或警报框?没有。您可以检查confirm命令的返回值是否确实为true或false,但无法检查是否存在 这些东西是浏览器的一部分,而不是DOM的一部分。我确信IE中存在一个肮脏的黑客,因为它是windows操作系统的一个私生子。确认和警报框正在阻止事件-Javascript代码在显示这些事件时停止执行。因此否-据我所知,您无法检测当前是否显示了一个警报。如果您想在触发警报()时运行一些代码,您可以尝试以下操作: 我只在C

有没有一种方法可以使用JavaScript或jQuery来检测是否显示确认框或警报框?

没有。您可以检查
confirm
命令的返回值是否确实为
true
false
,但无法检查是否存在


这些东西是浏览器的一部分,而不是DOM的一部分。我确信IE中存在一个肮脏的黑客,因为它是windows操作系统的一个私生子。

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

如果您想在触发
警报()时运行一些代码,您可以尝试以下操作:

我只在Chrome上测试过,所以我不确定是否支持浏览器

示例:

(函数(){
var\u old\u alert=window.alert;
window.alert=函数(){
//当警报弹出时运行一些代码
document.body.innerHTML+=“
警报”; _旧警报。应用(窗口、参数); //在警报后运行一些代码 document.body.innerHTML+=“
已完成警报
”; }; })(); 警惕(‘嘿’); 警惕(“你”); 警惕(“那里”);

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

如果您想

(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'));
    };

}());
这样做的缺点是

  • 您正在攻击浏览器的主机方法(您通常不应该做的事情-)
  • 你应该更好地跟踪你的
    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;
}
window.nativeAlert=window.alert;
window.alert=功能(消息){
var timeBefore=新日期();
var confirmBool=nativeAlert(消息);
var timeAfter=新日期();
如果((时间之后-时间之前)<350){
MySpecialDialog(“您已关闭警报,请将其重新打开或关闭!!!”;
}
}
window.nativeConfirm=window.confirm;
window.confirm=功能(消息){
var timeBefore=新日期();
var confirmBool=nativeConfirm(消息);
var timeAfter=新日期();
如果((时间之后-时间之前)<350){
MySpecialDialog(“您已关闭警报,请将其重新打开或关闭!!!”;
}
返回confirbool;
}

显然,我已将时间设置为3.5毫秒。但是在一些测试之后,我们只能在大约5毫秒内点击或关闭对话框,再加上@user113716的答案,你可以依赖时间。我假设,若确认对话框花费的时间少于200毫秒,那个么它就会被浏览器阻塞。下面我将在确认对话框被阻止时返回true(默认情况下返回false,代码为TypeScript)


你说的“被展示”到底是什么意思?你的意思是代码中是否有对该命令的引用?对于Web Workers,你可以…@Raynos我假设警报不会阻止Workers。@SimeVidas没有检测到警报。假设您想进行一些自动化测试,并断言警报在没有人工干预的情况下工作。你会怎么做?@Raynos事实证明,一个警报弹出窗口不会完全阻塞窗口。此演示--显示即使警报弹出窗口打开,工作人员与窗口和窗口与工作人员之间的通信也可以正常进行。+1因为您的第二段几乎完全正确。@Moses我要求您找到一个指向windows操作系统的链接,该链接允许您实际检查警报是否在IET中实际显示检查这一点的方法。据我所知,您可以做两件事-1)在
alert
confirm
(参见下面的答案)之前插入您自己的处理程序,或2)在间隔(
setInterval
)设置一个函数,以找出UI线程上次解锁的时间(尽管长延迟不一定仅仅来自
alert()
confirm())
这可能已经足够了。)@DanBeam我将这个问题解释为如何确保警报按预期显示在客户端上。这只是断言您的代码调用了
window.alert
函数。它不在DOM中,它不在你的手中。他们确实阻止了UI线程,但这并不意味着你可以嗅到它。你没有警报触发的garantuee。假设window.alert已正确实现@雷诺斯:如果你说的是浏览器支持,我想是的。我在回答中指出,我只在Chrome上测试过,除此之外我还不确定。还是我误解了你的评论?哦,我的意思是检测你的标准警报对话框是否在浏览器中实际显示。它在Firefox 89.0中工作。在confirm()中,如果时间太短,也可以返回null;调用方可能会检测到这一点,并且在大多数正常使用中看起来像“false”。在太短的时间内覆盖返回null(或false)也有助于防止在有人提交表单帖子但按住Enter键太长时意外自动确认。这不是个坏主意,但我担心会推翻用户的决定,这可能是因为他们知道会有一个确认。虽然我确实看到这样做的好处,因为这是这样做的一个漏洞。可以按住enter键的人将触发特殊对话框。但它会在99.9%的时间里起作用;是否强制用户阅读确认消息可能是特定于用例的。
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;
}
    let oldConfirm = window.confirm;
    window.confirm = (msg) => {
        let time = new Date().getTime();
        let conf = oldConfirm(msg);

        return new Date().getTime() - time > 200 ? conf : true;
    }