javascript/jquery: responding to a user clicking "ok" on an alert dialog

shealtiel picture shealtiel · Sep 1, 2011 · Viewed 54.9k times · Source

my code:

alert('Some message');

Question 1:

How to execute code that comes after alert() when user finished interacting with alert box?

Question 2:

How to detect if user pressed OK or Cancel on alert box ?

Answer

Darin Dimitrov picture Darin Dimitrov · Sep 1, 2011

Question 1:

The alert method blocks execution until the user closes it:

alert('Some message');
alert('doing something else after the first alert is closed by the user');

Question 2:

use the confirm function:

if (confirm('Some message')) {
    alert('Thanks for confirming');
} else {
    alert('Why did you press cancel? You should have confirmed');
}