I have some pages with forms in my application.
How can I secure the form in such a way that if someone navigates away or closes the browser tab, they should be prompted to to confirm they really want to leave the form with unsaved data?
I have some pages with forms in my application.
How can I secure the form in such a way that if someone navigates away or closes the browser tab, they should be prompted to to confirm they really want to leave the form with unsaved data?
You could check for a detailed explanation here: http://techinvestigations.redexp.in/comparison-of-form-values-on-load-and-before-close/ comparison-of-form-values-on-load-and-before-close
The main code:
Based on the previous answers, and cobbled together from various places in stack overflow, here is the solution I came up with which handles the case when you actually want to submit your changes:
It's worth noting that IE11 seems to require that the
closeEditorWarning
function returnsundefined
for it not to show an alert.The following one-liner has worked for me.
Just set
modified
to true or false depending on the state of your application.Short, wrong answer:
You can do this by handling the
beforeunload
event and returning a non-null string:The problem with this approach is that submitting a form is also firing the unload event. This is fixed easily by adding the a flag that you're submitting a form:
Then calling the setter when submitting:
But read on...
Long, correct answer:
You also don't want to show this message when the user hasn't changed anything on your forms. One solution is to use the
beforeunload
event in combination with a "dirty" flag, which only triggers the prompt if it's really relevant.Now to implement the
isDirty
method, there are various approaches.You can use jQuery and form serialization, but this approach has some flaws. First you have to alter the code to work on any form (
$("form").each()
will do), but the greatest problem is that jQuery'sserialize()
will only work on named, non-disabled elements, so changing any disabled or unnamed element will not trigger the dirty flag. There are workarounds for that, like making controls readonly instead of enabling, serializing and then disabling the controls again.So events seem the way to go. You can try listening for keypresses. This event has a few issues:
The
change
event also doesn't trigger on values set from JavaScript code, so also won't work for virtual inputs.Binding the
input
event to allinput
s (andtextarea
s andselect
s) on your page won't work on older browsers and, like all event handling solutions mentioned above, doesn't support undo. When a user changes a textbox and then undoes that, or checks and unchecks a checkbox, the form is still considered dirty.And when you want to implement more behavior, like ignoring certain elements, you'll have even more work to do.
Don't reinvent the wheel:
So before you think about implementing those solutions and all required workarounds, realize you're reinventing the wheel and you're prone to running into problems others have already solved for you.
If your application already uses jQuery, you may as well use tested, maintained code instead of rolling your own, and use a third-part library for all of this. jQuery's Are You Sure? plugin works great, see their demo page. It's as simple as this:
Custom messages not supported everywhere
Do note that Firefox 4 didn't support custom messages in this dialog. As of last month, Chrome 51 is being rolled out in which custom messages are also being removed.
Some alternatives exist elsewhere on this site, but I think a dialog like this is clear enough:
First of all, most browsers has this function by default. And why do you need this at all? Why not to keep the form synced? I mean, save it on any change without waiting any submitting from user. Like Google Contacts do. Of course if only all fields in form are mandatory. Users do not like when them force to fill something up without the opportunity to go away to think if they need it. :)