0

I want when i enter information into a form than i click to a hyperlink to go to another page without submit the form a message appear are you sure you want to move to another page if the user click yes he go to another page if he click no he will stay in the same page where the form exist.

And if the user go the page form and didn't enter any information in the field and click to move to another page the message didn't appear.

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

window.onload = function() {
    window.addEventListener("beforeunload", function (e) {
        if (formSubmitting) {
            return undefined;
        }

        var confirmationMessage = 'It looks like you have been editing something. '
                                + 'If you leave before saving, your changes will be lost.';

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

This code work if i enter information or not How can edit the code to when i didn't enter information the message don't appear.?

m7md
  • 231
  • 3
  • 11

1 Answers1

-1

use can below code

         <body onbeforeunload="return myFunction()">
                <script>
               function myFunction() {
               return "true"
               }
          </script>
          </body>