Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 251
shealtiel Avatar asked Sep 01 '11 19:09

shealtiel


People also ask

How do I click an OK button alert?

To click on the Ok button on alert, first of all we have to switch to alert with switchTo(). alert() method. Next, to click on the Ok button, we have to use accept() method.

Can we call alert from jQuery?

Unfortunately there is no built in one-liner for displaying a simple alert in jQuery UI, but it is easy to add an own one. This is a standard jQuery UI dialog displayed using one line of code. It follows the same theme as the rest of the jQuery UI widgets used on the site and it behaves like a normal modal dialog.

Does JavaScript alert stop execution?

One of the nice things about the built-in JavaScript alert is that - unlike virtually anything else in JavaScript - it's synchronous. It's completely blocking, and no other code will execute until it's been dismissed.

How do you alert a user in JavaScript?

Use the alert() function to display a message to the user that requires their attention. This alert box will have the OK button to close the alert box. The alert() function takes a paramter of any type e.g., string, number, boolean etc. So, no need to convert a non-string type to a string type.


3 Answers

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');
}
like image 187
Darin Dimitrov Avatar answered Sep 28 '22 07:09

Darin Dimitrov


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

like image 16
Nick Shaw Avatar answered Sep 29 '22 07:09

Nick Shaw


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

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

like image 12
rlemon Avatar answered Sep 28 '22 07:09

rlemon