I ran into this problem, so I would like to share my solution.
The Brent White solution does not work for me because I am using the jQuery-validation plugin. This means that if users provide invalid input, even after they click the submit button, they will still remain on the page. At this point, if they leave or refresh the page, a warning message will not be displayed.
$(window).bind('beforeunload', function(evt) { var isSubmitButton = (evt.srcElement.activeElement.type === "submit"); var isModified = ($form.data("isModified") === true); if ( !isSubmitButton && isModified) { return "You need to save your changes before you leave the page"; } });
source share