User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

If you want the behavior that when someone changes data in a form and doesn't submit it, that they are alerted they are about to leave the page without saving/submitting.  Jquery has some great functionality.

You will want your form submit to set the var used_submit  to something other than 0

<input type="submit" onclick="CleanLeavePage();" value="Save">

 

<script type="text/javascript">
var sform_clean;

var used_submit = 0;

// When the page load we get the values serialize
$(function() {
    sform_clean = $("form").serialize(); 
});

// Before we leave the page we now compare between the new form values and the orignal
window.onbeforeunload = function (e) {
    var sform_dirty = $("form").serialize();
    if(sform_clean != sform_dirty && used_submit ==0) {
        return 'You are about to leave a page where you have not saved the data';
    }
};
function CleanLeavePage(){
 used_submit = 1;
}
</script>