Monday, October 22, 2007

onBeforeUnload - confirm moving away from page

After reading Patricks post about onbeforeunload, I implemented the event in the CMS I mostly work with at Compendia.

His code required Prototype. I've made a little demo that doesn't require a JS-lib. I use two event-listeners ( onClick/onKeypress ) on document, and "set the page dirty" when the event "indicates" that a form element has gotten its value changed (look at the sourcecode of the page).

The only weakness I've found so far is that IE6 doesn't register keypress on comboboxes -> user could change a combobox-value with the keyboard and move away from the form unsaved without getting a warning.

>> Demo-page

I think onBeforeUnload is supported i FF2.x, IE 4+, and newer versions of Safari.

Mozilla has a simpler demo, where one field is tested.

Patrick has a downloadable Demo-NSF.