Search code examples
javascriptonbeforeunload

Submit form before user leaves page in javascript/jquery


Is it possible to create a more customized leaving-page event? Right now I am using the window.onbeforeunload event to warn users of their unsaved changes before leaving the page.

What I really want to do though, is to give the user the option to save their data right away before leaving the page(possibly with a button in a pop up) and then continue redirecting them. I assume it's not possible to let onbeforeunload do some extra work before displaying the message(I tried).

So is what I want even possible or a good idea?


Solution

  • The model used by most large sites (including stack) is simply to warn users that they have unsaved data. I suggest you replicate that behaviour.