Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect unsaved data in form when user leaves the page?

Tags:

jsf

I need to detect unsaved data in form when user leaves the page without submitting the form. I would like to implement that without adding a value change listener to each input.

This is the functional requirement:

"User open a page than click on any link if values in the page changed an alert message popup to notify user that he need to save changed data, but if did not change any thing system continue without notify user".

I tried to compare array method to compare both DTO coming for DB and the bind DTO, but it gives me a lot of problems in array length, and byte comparison.

like image 591
Sherif Omar Avatar asked Nov 24 '11 15:11

Sherif Omar


People also ask

How to display warning before leaving the web page with unsaved changes using JavaScript?

thisPage. closeEditorWarning = function (event) { if (window. thisPage. isDirty) return 'It looks like you have been editing something' + ' - if you leave before saving, then your changes will be lost.

How do I warn a user of unsaved changes before leaving a page in vue?

1. You can use vue lifecycle method - "beforeDestroy " hook as well and add a popup/dailog box before leaving page. 2 . You can use Vue-router BeforerouteLeave or use watch property on path.

What happens if the user does not save the form?

If the user doesn’t change anything, meaning the form data is identical to the data received from the server, the save button should not be displayed. If the user changes the data and leave the page without saving, a pop-up window is displayed, warning that there is unsaved data on the page, in order to prevent accidental loss of data.

How to warn the user that there is unsaved data?

When a user fills in many details in a form and then clicks back, or closes/refreshes the tab, we would like to warn the user that there is unsaved data on the page. We’d like the user to leave only if he agrees to the message below. The message will be looking like this: 1. When a user closes or refreshes the tab.

What does unsaved mean in edit form control?

11-14-2018 09:08 PM The Unsaved property of the Edit form control determines if the Edit form control contains user changes that have not been saved. If the EditForm1.Unsaved formula return true, it means that your Edit form contains some changes have not be saved.

What happens if the user changes the data and leaves without saving?

If the user changes the data and leave the page without saving, a pop-up window is displayed, warning that there is unsaved data on the page, in order to prevent accidental loss of data. Here’s an illustration of what we want to achieve:


1 Answers

This is normally implemented in the client side with help of JavaScript, because it's not nicely possible to intercept on beforeunload event from the server side on when the enduser leaves the page.

Here's a concrete example with help of the JavaScript library jQuery (otherwise you would end up with 10 times as much code to make it properly crossbrowser compatible and work seamlessly together with ajax re-renders):

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(function() {
        // Set the unload message whenever any input element get changed.
        $(':input').on('change', function() {
            setConfirmUnload(true);
        });

        // Turn off the unload message whenever a form get submitted properly.
        $('form').on('submit', function() {
            setConfirmUnload(false);
        });
    });

    function setConfirmUnload(on) {
        var message = "You have unsaved data. Are you sure to leave the page?";
        window.onbeforeunload = (on) ? function() { return message; } : null;
    }
</script>

Just paste this in your <h:head> template or just put it in some script.js file which you include by <h:outputScript>.

like image 198
BalusC Avatar answered Oct 18 '22 17:10

BalusC