Created
December 9, 2013 17:13
-
-
Save sdanna/7876115 to your computer and use it in GitHub Desktop.
Prevent a User Navigating Away from a Page with Unsaved Changes (http://www.jacopretorius.net/2013/12/prevent-a-user-navigating-away-from-a-page-with-unsaved-changes.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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