diff --git a/assets/js/upload.js b/assets/js/upload.js index 2be9b9bc..beab2c42 100644 --- a/assets/js/upload.js +++ b/assets/js/upload.js @@ -124,6 +124,26 @@ function setupImageUpload() { else { disableFetch(); } + + // Catch unintentional navigation away from the page + + function beforeUnload(event) { + // Chrome requires returnValue to be set + event.preventDefault(); + event.returnValue = ''; + } + + function registerBeforeUnload() { + window.addEventListener('beforeunload', beforeUnload); + } + + function unregisterBeforeUnload() { + window.removeEventListener('beforeunload', beforeUnload); + } + + fileField.addEventListener('change', registerBeforeUnload); + fetchButton.addEventListener('click', registerBeforeUnload); + form.addEventListener('submit', unregisterBeforeUnload); } export { setupImageUpload };