Detect if an alert or confirm is displayed on a page

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


Solution 1:

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

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

Example: 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.

Solution 2:

No there is not. You can check that the return value of a confirm command is indeed true or false but you cant check whether there visually there.

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.