Last active
August 29, 2015 14:05
-
-
Save noel9999/b200847503fe5879c48d to your computer and use it in GitHub Desktop.
The onbeforeunload way with turbolinks.
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
// 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