Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 941
Uggers2k Avatar asked Feb 01 '11 19:02

Uggers2k


People also ask

How do I inspect alerts in Chrome?

Right Click on the webpage in Chrome Browser and click "Inspect" .

How do you display alerts in HTML?

The Window alert() method is used to display an alert box. It displays a specified message along with an OK button and is generally used to make sure that the information comes through the user. It returns a string which represents the text to display in the alert box.

What is difference between alert and window alert?

Difference between alert() and window. alert()window in JS is a global object, that means you can access it anywhere throughout your code. So, alert() becomes a global method / function. Therefore, you can call alert function of window object as window.

What are alert and confirm method in JavaScript?

JavaScript Message Boxes: alert(), confirm(), prompt() JavaScript provides built-in global functions to display messages to users for different purposes, e.g., displaying a simple message or displaying a message and take the user's confirmation or displaying a popup to take the user's input value.


3 Answers

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.

like image 60
user113716 Avatar answered Oct 10 '22 23:10

user113716


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.

like image 44
Raynos Avatar answered Oct 10 '22 22:10

Raynos


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

  • you're hacking a host method of a browser (stuff you shouldn't typically do - http://perfectionkills.com/whats-wrong-with-extending-the-dom/)
  • you should just be better keeping track of your alert() confirm() usage, haha
like image 42
Dan Beam Avatar answered Oct 10 '22 23:10

Dan Beam