Here is a bit of jQuery script to warn the user if they want to leave the page but have changed data in a form without saving.
// this warns user when trying to leave page with unsaved data
var warnOnLeavingUnsavedData = true;
var dataChanged = false;
$('form').on('keyup change', 'input, select, textarea', function () {
formChange();
});
function formChange()
{
dataChanged = true;
$('form').submit(function () { dataChanged = false; });
}
$(window).bind('beforeunload', function () {
if (warnOnLeavingUnsavedData) {
if (dataChanged) {
return;
}
}
});