shealtiel
shealtiel

Reputation: 8388

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

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 ?

Upvotes: 28

Views: 61230

Answers (3)

rlemon
rlemon

Reputation: 17667

var r = confirm("Press a button!");
if (r == true) {
    alert("You pressed OK!");
}
else {
    alert("You pressed Cancel!");
}

http://jsfiddle.net/rlemon/epJGG/

Upvotes: 12

Nick Shaw
Nick Shaw

Reputation: 2113

The code after the alert() call won't be executed until the user clicks ok to the alert, so just put the code you need after the alert() call.

If you want a nicer floating dialog than the default javascript confirm() popup, see jQuery UI: floating window

Upvotes: 17

Darin Dimitrov
Darin Dimitrov

Reputation: 1039468

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

Upvotes: 49

Related Questions