First of all, I am new to Javascript and Jquery. I need a function that warns user before leaving page with unsaved changes. I know that similar questions have been issued before but I could never get the page display message when I apply method mentioned in threads. Following question is most popular in the field, so I have mostly exploited methods from the thread:
Warn user before leaving web page with unsaved changes
Here is what I got in my page:
<gt-form:form commandName="manager" method="GET" id="managerForm" name="managerForm">
//Form fields,buttons
</gt-form:form>
<script>
var formSubmitting = false;
var setFormSubmitting = function() {
debugger;
formSubmitting = true;
};
</script>
<script>
window.onload = function() {
window.addEventListener("beforeunload", function(e) {
var confirmationMessage = 'It looks like you have been editing something. ';
confirmationMessage += 'If you leave before saving, your changes will be lost.';
if (formSubmitting || !isDirty()) {
return undefined;
}
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
});
};
</script>
<script>
$('#managerForm').data('serialize',$('#managerForm').serialize()); // On load save form current state
var isDirty = function() { $(window).bind('beforeunload', function(e){
if($('#managerForm').serialize()!=$('#managerForm').data('serialize')) isDirty=true;
else e=null; // i.e; if form state change show warning box, else don't show it.
}); };
</script>
What am I doing wrong?