I'm having an application with an interactive canvas and want to save changes on it, before the user exits the page.
My approach
function saveBeforeUnload(){
if (confirm('Do you want to save the current state to clipboard?')) {
/*yes*/ if (canvas.getObjects("rect").length > 0){
localStorage.setItem("clipboard_unfinishedconfig", JSON.stringify(canvas.toJSON(customProperties)));
return;
/*no:*/ } else {
localStorage.setItem("clipboard_unfinishedconfig", "");
return;
}
}
I call it by
window.onbeforeunload = saveBeforeUnload;
What I need to accomplish is a yes/no confirmation, if the user wants to ovverride the localStorage Item with the current configuration.
Problem
With my code, the confirm does not appear. Accordingly the localStorage is empty... console says "Blocked confirm..."
Approach - I
Explanation:
window.onbeforeload
executes whatever is in the handler but it really cares about the return statement which shall be used as confirmation message. And Ofcourse we can't change the button labels. Once theonbeforeunload
dialog is shown it blocks everything(that's why your prompt is blocked). So in the following code what we are doing is that we are scheduling a save using asetTimeout
by giving 0 milliseconds, so that it is added to the event loop.Now if the user decides to close the tab anyway that
setTimeout
handler never runs, it he chooses to stay the handler runs & the changes are saved.Well, you can do the following:
So, If the user chooses to stay back, the changes will be saved,
This is a working solution, but not the best user experience in my opinion, so what I suggest is that you keep auto saving the changes as the user makes & give a button to reset the canvas if required. Also you should not save on every single change, but instead keep auto saving in a specific interval of time. If the user tries to close between that interval then show this dialog saying "you have pending changes left".
Approach - II ( Your way )
But this will be to many nagging dialogs.
Hope this helps.