Last active
December 1, 2017 09:33
-
-
Save krisleech/5562f85a81686a5d3a775ed177b6782e to your computer and use it in GitHub Desktop.
Checking for unsaved changes
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
// this does not work for inputs which do not propergate their change event, e.g. when using a JS calendar. Use a HTML5 date input instead. | |
$(document).on('change', 'input, select, textarea', function(e) { | |
if ($(this).is('.ignore-unsaved-changes')) { return; } | |
$(this).addClass('changed-input'); | |
}); | |
// for ajax clicks we need to check for changed inputs | |
// it might be possible to do this universally via an ajax hook (something like beforeAjax) | |
$('.tab').click(function(event){ | |
// check for unsaved changes in current tab | |
if($('input.changed-input').length) { | |
if(confirm('You have unsaved changes, do you want to continue?') == false) { | |
// do not load the panel | |
event.stopPropagation(); | |
event.stopImmediatePropagation(); | |
event.preventDefault(); | |
return false; | |
} | |
else | |
{ | |
;; do ajax | |
} | |
} | |
else | |
{ | |
;;do ajax | |
} | |
}); | |
// non-ajax links | |
window.onbeforeunload = function() { | |
if($('input.changed-input').length) { | |
return "Are you sure you want to leave?" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment