Skip to content

Instantly share code, notes, and snippets.

@ducas
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save ducas/41c9a6c797dfc65b7a74 to your computer and use it in GitHub Desktop.

Select an option

Save ducas/41c9a6c797dfc65b7a74 to your computer and use it in GitHub Desktop.
Confirm Navigation Dialog
(function ($) {
$.fn.preventNavigation = function () {
var that = this;
this.on('change', 'input, select, textarea', function () {
that.data('field-change', true);
});
this.on('submit', function () {
if (that.valid()) {
that.data('field-change', false);
}
});
window.addEventListener("beforeunload", function (e) {
if (that.data('field-change')) {
var confirmationMessage = "You have unsaved changes. Are you sure you want to leave this page?";
(e || window.event).returnValue = confirmationMessage; //Gecko, IE, Firefox, etc.
return confirmationMessage; //Webkit, Safari, Chrome etc.
}
return;
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment