JS Function warning before closing browser I have a function that makes sure the user wants to actually leave the page. I am not a jQuery expert. Is the code below the most efficient way to do this function? Or can it be simplified much more to run smoother? Any advice is greatly appreciated.

$(document).ready(function() {
  formmodified=0;
    $('form *').change(function(){
        formmodified=1;
    });
  window.onbeforeunload = confirmExit;
    function confirmExit() {
      if (formmodified == 1) {
          return "Are you sure you want to leave this page? This will abandon any progress on changes to document preferences";
      }
  }
  $("input[name='ModifyRecord']").click(function() {
        formmodified = 0;
    });
    $("input[name='DeleteRecord']").click(function() {
        formmodified = 0;
    });
    $("input[name='DeleteChecked']").click(function() {
        formmodified = 0;
    });
});
If you don't need the confirmExit function anywhere else, you can just assign it to to window.beforeunload.

window.onbeforeunload = function() {
    if (formmodified == 1) {
        return "Are you sure you want to leave this page? This will abandon any progress on changes to document preferences";
    }
}


Make sure your indentation is consistent, it's confusing to read when brackets don't match up.

Last but not least, it might be helpful (saves you the trouble of writing the same thing multiple times) and more performant to use a class for elements that should have a click handler to reset the "modified" state of the form:

$("input.formreset").click(function() {
    formmodified = 0;
});