217
Views
3
Comments
Solved
Trigger Save Using Javascript during Unload
Question

Hello Guys,


During Unloading Web Screen wherein I need to trigger a Save Function (thru the Button Click) to save modified information.

Been reading posts again about beforeunload and addeventlisteners, the thing here is that, is this doable thru javascripts? Or other strategy you can suggest to this.


Hoping you guys shed some light about this.


Many thanks and regards,

Christopher

2020-02-28 09-46-54
Eduardo Jauch
Solution

Hi Christopher,

I will say that at first, no, this is not possible. Nor is this desirable.

Via JavaScript you can present a message to the user saying that if he leaves, it will lose his work.

You can also try to save data at short intervals, using a JavaScript timer and FakeNotify, but this should always be optional for the user (and for sure will impact performance).

That's it, I think.

In any case, there will be situations where you will not be able to save it anyway, like Power failures and "process kill".

Also, what would happen if the work is in a state with wrong data.

Or even worse, if the session expired already?

So, saving automatically would be a bad idea. 

Cheers

2024-10-25 09-14-42
Christopher Bautista

Eduardo Jauch wrote:

Hi Christopher,

I will say that at first, no, this is not possible. Nor is this desirable.

Via JavaScript you can present a message to the user saying that if he leaves, it will lose his work.

You can also try to save data at short intervals, using a JavaScript timer and FakeNotify, but this should always be optional for the user (and for sure will impact performance).

That's it, I think.

In any case, there will be situations where you will not be able to save it anyway, like Power failures and "process kill".

Also, what would happen if the work is in a state with wrong data.

Or even worse, if the session expired already?

So, saving automatically would be a bad idea. 

Cheers

Hi Ed,

Thanks for the response...

About the Javascript timer, do you have a sample on how to implement such?

Thanks and regards,

Chris


2024-10-25 09-14-42
Christopher Bautista

Hi Everyone,

I'm trying to get the value of an Input Parameter as shown in the below sample.

I'm not sure if I'm doing it correctly.


var modifiedForm = 0;
var inputWidgetID;
var isMissing;

$(document).load(function(){
modifiedForm = 1;
window.onbeforeunload = confirmExit;})

$(document).ready(function(){
   
    $(':input').change(function(){
       
        modifiedForm = 1;
       
        var elemid = $('.autobutton').attr('id');
        var elem = document.getElementById(elemid);
           
        if (typeof elem.onclick == 'function')
        {                
            elem.onclick.apply(elem);
        }
       
        inputWidgetID = $('.inputmissingmf').attr('id');
        isMissing = document.getElementById(inputWidgetID).val();
       
        alert(isMissing + ' ');
       
    });
   
    window.onbeforeunload = confirmExit;
   
    function confirmExit(){
       
       
         
        return isMissing;
   
     
   
   }
   
    $('.Button').click(function(){
            modifiedForm = 0;
    });
});


Thanks and regards,

Chris

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.