In IE and FF, i can attach an event handler to onBeforeUnload, and by passing a string to a property of the event, the user will see a dialog asking him whether he wants to
This question is covered in slightly more detail in another newer StackOverFlow question: Setting onbeforeunload on body element in Chrome and IE using jQuery
StackOverflow itself uses onbeforeunload
, and it works fine for me in Safari:
function setConfirmUnload(a){window.onbeforeunload=a?function(){return a}:null}
I checked it in chrome and it seems to be working fine using this:
<body onunload="alert('x');" onbeforeunload="test1();">
This works perfectly for me in both Chrome and Safari:
<html><body><p>Test</p>
<script>window.onbeforeunload = function() { return "Sure?"; }</script>
</body></html>
When I try to close the window, I get the prompt.