James123
James123

Reputation: 11652

ask for saving before leaving the page?

based on this post I am asking confirmation from user to save data before leaving the page,if user enter any data in the textfields.

but I am getting below error when page loads and my form id is "Form1"

enter image description here

<script type="text/javascript" language="javascript">
     window.onload = function (e) {
        var form_has_been_modified = 0;
          $("form[id^='Form1']").on("change keyup", ":input", function () {
          form_has_been_modified = 1;
        })
        };
     window.onbeforeunload = function (e) {
           if (!form_has_been_modified) {
             return;
        }
        var message = "Do you want to save the data before leave the page?";
        var e = e || window.event;

         // For IE and Firefox prior to version 4
        if (e) {
              e.returnValue = message;
        }
        // For Safari
         return message;
             };


        </script>

how to solve this issue?

Updated: in HTML source code shows <form name="aspnetForm" method="post" action="EditMember.aspx?Id=3664653" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm" enctype="multipart/form-data">, How to use this form in above code?

Upvotes: 0

Views: 1042

Answers (2)

msEmmaMays
msEmmaMays

Reputation: 1063

Your selector is wrong

$("form[id^='Form1']") Should be $("#Form1")

Also don't forget there might be more then just input tags (select and textarea for example)

Upvotes: 0

VisioN
VisioN

Reputation: 145398

Method on() was added to jQuery version 1.7.

It seems that you are using version 1.4.1. Try to update jQuery and the error should disappear.

Upvotes: 4

Related Questions