From 935199121446cc431c840f39185050f2610326c9 Mon Sep 17 00:00:00 2001 From: Timon Foerster Date: Tue, 31 May 2022 12:16:18 +0200 Subject: [PATCH] (#144) bugfix and some changes --- root/static/js/unsavedChanges.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/root/static/js/unsavedChanges.js b/root/static/js/unsavedChanges.js index 15c0e31c..4b528db1 100644 --- a/root/static/js/unsavedChanges.js +++ b/root/static/js/unsavedChanges.js @@ -1,21 +1,22 @@ +"use strict"; + const forms = document.getElementsByClassName('confirmIfUnsavedChanges'); let hasUnsavedChanges = false; -for(const form of forms) { - const inputs = Array.prototype.slice.apply(form.getElementsByTagName('input')); - const textareas = Array.prototype.slice.apply(form.getElementsByTagName('textarea')); - const selects = Array.prototype.slice.apply(form.getElementsByTagName('select')); - const elements = inputs.concat(textareas).concat(selects); - console.log(elements) - for(const elem of elements) { - elem.addEventListener('input', () => {if(!hasUnsavedChanges) hasUnsavedChanges = true}); +for( const form of forms ) { + const inputs = Array.prototype.slice.apply(form.getElementsByTagName('input')); + const textareas = Array.prototype.slice.apply(form.getElementsByTagName('textarea')); + const selects = Array.prototype.slice.apply(form.getElementsByTagName('select')); + const elements = inputs.concat(textareas).concat(selects); + + for( const elem of elements ) { + elem.addEventListener('input', () => { if( !hasUnsavedChanges ) hasUnsavedChanges = true }); } - form.addEventListener('submit', () => {if(hasUnsavedChanges) hasUnsavedChanges = false}); + + form.addEventListener('submit', () => { if( hasUnsavedChanges ) hasUnsavedChanges = false }); } +// See documentation about 'beforeunload' event on https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event window.addEventListener('beforeunload', (e) => { - if (hasUnsavedChanges) { - (e || window.event).returnValue = ''; - return ''; - } -}) \ No newline at end of file + if( hasUnsavedChanges ) e.preventDefault(); // +});