检测页面上是否显示警报或确认 [英] Detect if an alert or confirm is displayed on a page

查看:119
本文介绍了检测页面上是否显示警报或确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

我是仅在Chrome中测试过,因此我不确定浏览器支持。

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

示例: 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');

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

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.

这篇关于检测页面上是否显示警报或确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆