0

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?

Community
  • 1
  • 1
metzelder
  • 655
  • 2
  • 15
  • 35

3 Answers3

2

I have implemented that way, and it worked:

var somethingChanged=false;
        $('#managerForm input').change(function() { 
            somethingChanged = true; 
       }); 
        $(window).bind('beforeunload', function(e){
            if(somethingChanged)
                return "You made some changes and it's not saved?";
            else 
                e=null; // i.e; if form state change show warning box, else don't show it.
        });
    });

The thing is to binding changed event of my form field with form-id.

metzelder
  • 655
  • 2
  • 15
  • 35
0

Try this:

        var formSubmitting = false;
        var setFormSubmitting = function() {
            debugger;
            formSubmitting = true;
        };

    window.onload = function() {
        window.addEventListener("beforeunload", function(e) {
            console.log('beforeunload');
            var confirmationMessage = 'It looks like you have been editing something. ';
            confirmationMessage += 'If you leave before saving, your changes will be lost.';
            var isDirty = false;
            //Here we are checking the condition
            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.


            if (formSubmitting || !isDirty) {
                return undefined;
            }

            (e || window.event).returnValue = confirmationMessage; //Gecko + IE
            return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
        });
    };
jayapal
  • 67
  • 10
0

$(function(){
 $('#myForm').data('serialize',$('#myForm').serialize()); 
 $(window).bind('beforeunload', function(e){
  console.log($('#myForm').serialize(), $('#myForm').data('serialize'));
  if($('#myForm').serialize()!=$('#myForm').data('serialize'))
   return "You made some changes and it's not saved?";
  else 
   e=null; // i.e; if form state change show warning box, else don't show it.
 });
});
function saveData() {
 $('#myForm').data('serialize',$('#myForm').serialize());
 // and save the same in your database through ajax call. So that you won't have prompt once you save the data
}
<form id="myForm" name="myForm" action="" method="post" onsubmit="return saveData();">
  <input type="text" name="firstName" />
  <input type="text" name="lastName" />
  <select name="myOptions">
 <option value="">--Select--</option>
 <option value="volvo">Volvo</option>
 <option value="saab">Saab</option>
 <option value="mercedes">Mercedes</option>
 <option value="audi">Audi</option>
 </select>
  <input type="submit" value="Submit"/>
</form>
arun bahal
  • 328
  • 3
  • 13