Saturday, April 21, 2007

TIPS - Warning before navigate away from a web form without save

My users often click on a link or change page when they fill a form without save it before. Then, they lose all the informations.

With Lotus Notes client they have to confirm to exit the page. You can do the same thing on Web.



When to display the warning message ?
I prefer display the message only if the user have change the form. If you display the warning every time, the user will finish to ignore it. To detect if the form has been changed, I use an hidden field who his value is changed when one field of the form is changed:

<input type="hidden" name="isChanged" id="isChanged" value="0">

To modifiy the value of this field I use javascript function. You can declare this function on the header of your form (or subform) or in Javascript library. This function can looks like that :
function formChange(){
document.forms[0].isChanged.value="1";
}

You just have to add a call to this function on the "onChange" event of the fields you want to control.
How to detect the user navigate away ?
To detect if a user navigate away the web page, you have to add the event onBeforeUnload. To do that, add to the header of our Notes form :
window.onbeforeunload = confirmExit;

Where confirmExit is a Javascript function who control if it's necessary to display the warning and where you can customize the message.
function confirmExit() {
if(document.forms[0].isChanged.value=='1')
return "You've not save the form. Are you sure ?";
}

The first condition verify the hidden field of control to know if the form has been changed.

7 comments:

Patrick Kwinten said...

Hej!

Very neat and simple solution. I haven't used the window.onbeforeunload event before.

Thanks for the tip!

Chris Whisonant said...

Thanks for the information. I posted about this in relation to document locking.

From my reading, I found that with Domino, it may help out to declare the onbeforeunload event in the HTML Body Attributes:

"onbeforeunload=\"beforeunload();\""

But I've had some issues with using this alongside document locking. Specifically, it appears that I can't trap for if the user clicks "OK" on the message window asking if they wish to close it. If OK is clicked, the popup happens again. I detail that issue here.

But, practically, in 9 months with this active in a particular application with about 43,000 docs now, I have only had to manually unlock 1 document. So just having the window helps to remind the user to save the changes.

Do you have any tips for me? Thanks!

Benoit Dubuc said...

Just beware that you don't have complete control over the message that is displayed in that box. In your screen shot, it shows that you cannot change/remove the top and bottom lines.

I used this technique in a bilingual application. The users were a bit confused as they were using English IE in a French application, so they had a message like this:

Are you sure you want to navigate away from this page ?

Le document n'est pas auvegardé.

Pres OK to continue....


You get the picture.

Too bad we can't control the whole message.

Mohammed AbdelRahman said...

great, thanks!
but when the user sumbits the form, the same warning appears.

Mohammed AbdelRahman said...

perhaps i can do a
form.onsubmit=window.onbeforeunload=null;

Philippe GAUVIN said...

You can empty your control field in the onClick event of your Save button or link.

barboul said...

Thanks, it is working perfectly fine! :)
Have a great day!