开发者

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

开发者 https://www.devze.com 2023-04-01 03:55 出处:网络
my code: alert(\'Some message\'); Question 1: How to execute code that comes after alert() when user finished interacting with alert box?

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号