views:

159

answers:

3

I want to write JavaScript code that would, when I close the current HTML page, display an alert message like "Are you sure?"

I want to take the value of the button from the alert message, whatever the user pressed. How can I do this?

+3  A: 

You can use window.onbeforeunload

rahul
A: 

The following will return a true or false depending on user input you can act on it as you like.

confirm("Are U Sure ?")

To tie something to the page close you can do the following

window.onbeforeunload = function() { 
  if(confirm("Are U Sure ?"))
  { 
    return true;
  }
  else
  {
    return false;
  } 
}; 
Jeff Beck
+1  A: 

As far as I know, beforeunload is never fired in Opera.

For supporting that browser, you probably have to build some page-leaving-detection engine yourself (observing links etc).

npup