Javascript: Detect when an alert box is OK'ed and/or closed

Since alert is blocking:

alert('foo');
function_to_call_when_oked_or_closed();

Just put the function after the call to alert.


You can show a confirm box that displays a message with an OK and a Cancel button and check which button the user clicked by:

<script type="text/javascript">

var answer = confirm ("Is this working for you?")
if (answer)
    alert ("Woo Hoo! Then my answer was correct.")
else
    alert ("Darn. Well, keep trying then.")

</script>

If you want to make use of a simple alert box you can have a look at 1001 tutorials online such as this one for example. But your question does not specify how exactly you want to implement your alert.

http://www.tizag.com/javascriptT/javascriptalert.php


If user didn't press ok or close, the JS will not proceed to the next line. So actually there is no need to detect this.

Maybe you want to know if the user pressed ok or close. For the alert popup box, there is no easy way to tell it is ok'ed or closed. In Firefox, you don't even have the close button.

So if you really want to do this, you can use Confirm Box or Prompt Box. Please check the link below for how to use Confirm Box or Prompt Box: http://www.w3schools.com/js/js_popup.asp