Skip to content

Instantly share code, notes, and snippets.

@noel9999
Last active August 29, 2015 14:05
Show Gist options
  • Save noel9999/b200847503fe5879c48d to your computer and use it in GitHub Desktop.
Save noel9999/b200847503fe5879c48d to your computer and use it in GitHub Desktop.
The onbeforeunload way with turbolinks.
// Without turbolinks, it works fine!
$(document).ready(function(){
formmodified=0;
$('form *').change(function(){
formmodified=1;
});
window.onbeforeunload = confirmExit;
function confirmExit() {
if (formmodified == 1) {
return "資料有做修改,確定直接離開頁面嗎?";
}
}
$("input[name='commit']").click(function() {
formmodified = 0;
});
});
// With turbolinks, we try this... But got failed...
$(document).on('ready page:load',function(){
formmodified=0;
$('form *').change(function(){
formmodified=1;
});
$(window).on('page:before-change', confirmExit);
function confirmExit() {
if (formmodified == 1) {
return "資料有做修改,確定直接離開頁面嗎?";
}
}
$("input[name='commit']").click(function() {
formmodified = 0;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment