0

Requirement: I am attempting to alert the user when they leave a page that their changes will be lost.

Problem: I am using webforms and the only solution I have seen to alert the user is using javascript's 'window.onbeforeunload'. This would work great if it didn't fire every time a postback occurred.

Page description: This is a page inherits in a masterpage. The master page is simply a navigation bar with a few hyperlinks. The content page consist of numerous text boxes and a few drop down select boxes.

Question: How do I determine if the javascript event window.onbeforeunload is fired by a postback or not (dirty or not)?

Thank you guys, this is my first question asked. Hopefully I was descriptive enough. I am not allowed to share all of the code due to company policy. Any question I will be happy to answer.

  • Well, I think the solution is...to use `onbeforeunload`. You can, however, make the function detect whether the page is "dirty" or not (so, not on postback) and return an appropriate value which, in turn, would either trigger the dialog or not. – VLAZ Sep 13 '16 at 20:42
  • possible duplicate - http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch – MPawlak Sep 13 '16 at 20:45
  • See: http://stackoverflow.com/questions/4787995/how-to-disable-beforeunload-action-when-user-is-submitting-a-form – stephen.vakil Sep 13 '16 at 20:45
  • Thanks for the quick responses guys – JohansenJr Sep 13 '16 at 20:48

1 Answers1

0

For this We need to check whether any Input fields or form got edited.

Example: function isDirty(form) {

for (var i = 0; i < form.elements.length; i++) {
    var element = form.elements[i];
    var type = element.type;
 switch (type) {

        case "checkbox":
        case "radio":{
            if (element.checked != element.defaultChecked) {
                return true;
            }
            break;
        }
      }

}