views:

1160

answers:

1

I have a page with a form that is submittes via ajaxSubmit() (so, without changing the page).

My goal is that, when the user try to change page (or even to close the browser), i ask him if really want to exit the page without sending the form (exactly as gmail does).

Gmail for example do this with a window.confirm-like popup, but if it is possible, i'll like to handle it with custom messages and options.

jQuery have the unload event:

$(window).unload( function () { alert("Bye now!"); } );

but it permits me just to do something before exit the page; i need to 'block' the page exit, if the user click the relative button.

So, how to handle (and cancel) the page-exit event?

+2  A: 

try the following. Demo here

<script type="text/javascript">

       function unloadPage(){
           return "dont leave me this way";
       }

       window.onbeforeunload = unloadPage;

</script>
redsquare
Yes, thats it! Im sorry i cant customize the 'alert', but i dont think will be a big problem.
DaNieL