I want to warn a user when he is about to discard changes in a form when switching to another page.
Is if (!confirm("Are you sure")) return false;... kind of message considered good practice?
Should I use a modalpanel for that? (More work)
Thanks!
There are two pieces to this question:
confirm()is considered good practice.The two principles on which to judge the first part are: (1) never throw away user input and (2) don’t halt a user’s flow.
confirm()dialogs are modal, which means they stop users in their tracks. They are forced to answer the question without moving on. Generally, this is bad. If you can do it in a way that doesn’t stop the user’s flow, that is better. (For example, storing the input in a cookie or on the server or emailing a link to the user to finish the form later.) If you can’t find another way, a modal dialog might be better than losing the data entered.For the second part it depends on your implementation whether this will work or not. I have a feeling it won’t because the user can always type something into the address bar to get away from your page. The surest way to prompt a user before leaving the page no matter the escape vector is to use the
windowobject’sonbeforeunloadevent and return a string. Implementation details at the Mozilla Developer Network.