Skip to content

Instantly share code, notes, and snippets.

@sdanna
Created December 9, 2013 17:13
Show Gist options
  • Save sdanna/7876115 to your computer and use it in GitHub Desktop.
Save sdanna/7876115 to your computer and use it in GitHub Desktop.
$.fn.extend({
trackChanges: function() {
$(":input", this).keyup(function() {
$(this.form).data("changed", true);
});
var form = $(this);
$(form).submit(function() {
form.data("submitted", true);
});
},
isChanged: function() {
return this.data("changed") && !this.data("submitted");
}
});
$(document).ready(function() {
if ($('.detect_changes').length > 0) {
$('.detect_changes').trackChanges();
$(window).on('beforeunload', function() {
if ($('.detect_changes').isChanged()) {
return 'Your changes have not been saved.';
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment