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开发者_JAVA百科 box ?
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');
}
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
var r = confirm("Press a button!");
if (r == true) {
alert("You pressed OK!");
}
else {
alert("You pressed Cancel!");
}
http://jsfiddle.net/rlemon/epJGG/
精彩评论