JQuery ajax cleanup code before closing page











up vote
1
down vote

favorite












Before closing/navigating away from my web page I need to call a 'cleanup code' function using jQuery AJAX. If, and only if, the clean up code fails, I want to display an alert and prevent closing/navigation. Otherwise the page should close successfully without any confirmation dialog.



I called my cleanup AJAX in an onbeforeunload event handler, and omitted the return statement from the handler to prevent the confirmation dialog. The cleanup works, but now I can't alert to the user in case of clean up failure!



Any suggestions on how to alert to the user on failure and prevent closing, please?



window.onbeforeunload =  function(e) { 
MyCleanUpCode();
};









share|improve this question




















  • 1




    That sequence is not possible within beforeunload. You must determine synchronously whether to show the prompt or not
    – charlietfl
    Nov 11 at 11:31








  • 1




    Suggest you reconsider approach and do cleanup on periodic interval and use some sort of dirty flag when user changes something in between cleanups and use that flag to show unload prompt. Also you can not use an alert() inside that event handler
    – charlietfl
    Nov 11 at 11:39








  • 1




    Not really. Your choices are show the prompt or not and that is about all you can control. If the ajax is critical you need to take care of it before user tries to leave
    – charlietfl
    Nov 11 at 11:49








  • 2




    I know that's what you would like but it is just not that simple. What if users browser crashes or they lose power? That event and ajax would never even occur.
    – charlietfl
    Nov 11 at 11:57






  • 1




    Safest approach is do server updates or localStorage updates for every change user makes then there is no cleanup needed
    – charlietfl
    Nov 11 at 12:00

















up vote
1
down vote

favorite












Before closing/navigating away from my web page I need to call a 'cleanup code' function using jQuery AJAX. If, and only if, the clean up code fails, I want to display an alert and prevent closing/navigation. Otherwise the page should close successfully without any confirmation dialog.



I called my cleanup AJAX in an onbeforeunload event handler, and omitted the return statement from the handler to prevent the confirmation dialog. The cleanup works, but now I can't alert to the user in case of clean up failure!



Any suggestions on how to alert to the user on failure and prevent closing, please?



window.onbeforeunload =  function(e) { 
MyCleanUpCode();
};









share|improve this question




















  • 1




    That sequence is not possible within beforeunload. You must determine synchronously whether to show the prompt or not
    – charlietfl
    Nov 11 at 11:31








  • 1




    Suggest you reconsider approach and do cleanup on periodic interval and use some sort of dirty flag when user changes something in between cleanups and use that flag to show unload prompt. Also you can not use an alert() inside that event handler
    – charlietfl
    Nov 11 at 11:39








  • 1




    Not really. Your choices are show the prompt or not and that is about all you can control. If the ajax is critical you need to take care of it before user tries to leave
    – charlietfl
    Nov 11 at 11:49








  • 2




    I know that's what you would like but it is just not that simple. What if users browser crashes or they lose power? That event and ajax would never even occur.
    – charlietfl
    Nov 11 at 11:57






  • 1




    Safest approach is do server updates or localStorage updates for every change user makes then there is no cleanup needed
    – charlietfl
    Nov 11 at 12:00















up vote
1
down vote

favorite









up vote
1
down vote

favorite











Before closing/navigating away from my web page I need to call a 'cleanup code' function using jQuery AJAX. If, and only if, the clean up code fails, I want to display an alert and prevent closing/navigation. Otherwise the page should close successfully without any confirmation dialog.



I called my cleanup AJAX in an onbeforeunload event handler, and omitted the return statement from the handler to prevent the confirmation dialog. The cleanup works, but now I can't alert to the user in case of clean up failure!



Any suggestions on how to alert to the user on failure and prevent closing, please?



window.onbeforeunload =  function(e) { 
MyCleanUpCode();
};









share|improve this question















Before closing/navigating away from my web page I need to call a 'cleanup code' function using jQuery AJAX. If, and only if, the clean up code fails, I want to display an alert and prevent closing/navigation. Otherwise the page should close successfully without any confirmation dialog.



I called my cleanup AJAX in an onbeforeunload event handler, and omitted the return statement from the handler to prevent the confirmation dialog. The cleanup works, but now I can't alert to the user in case of clean up failure!



Any suggestions on how to alert to the user on failure and prevent closing, please?



window.onbeforeunload =  function(e) { 
MyCleanUpCode();
};






javascript jquery onbeforeunload






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 11:52









Rory McCrossan

239k29203244




239k29203244










asked Nov 11 at 11:25









Nina

3910




3910








  • 1




    That sequence is not possible within beforeunload. You must determine synchronously whether to show the prompt or not
    – charlietfl
    Nov 11 at 11:31








  • 1




    Suggest you reconsider approach and do cleanup on periodic interval and use some sort of dirty flag when user changes something in between cleanups and use that flag to show unload prompt. Also you can not use an alert() inside that event handler
    – charlietfl
    Nov 11 at 11:39








  • 1




    Not really. Your choices are show the prompt or not and that is about all you can control. If the ajax is critical you need to take care of it before user tries to leave
    – charlietfl
    Nov 11 at 11:49








  • 2




    I know that's what you would like but it is just not that simple. What if users browser crashes or they lose power? That event and ajax would never even occur.
    – charlietfl
    Nov 11 at 11:57






  • 1




    Safest approach is do server updates or localStorage updates for every change user makes then there is no cleanup needed
    – charlietfl
    Nov 11 at 12:00
















  • 1




    That sequence is not possible within beforeunload. You must determine synchronously whether to show the prompt or not
    – charlietfl
    Nov 11 at 11:31








  • 1




    Suggest you reconsider approach and do cleanup on periodic interval and use some sort of dirty flag when user changes something in between cleanups and use that flag to show unload prompt. Also you can not use an alert() inside that event handler
    – charlietfl
    Nov 11 at 11:39








  • 1




    Not really. Your choices are show the prompt or not and that is about all you can control. If the ajax is critical you need to take care of it before user tries to leave
    – charlietfl
    Nov 11 at 11:49








  • 2




    I know that's what you would like but it is just not that simple. What if users browser crashes or they lose power? That event and ajax would never even occur.
    – charlietfl
    Nov 11 at 11:57






  • 1




    Safest approach is do server updates or localStorage updates for every change user makes then there is no cleanup needed
    – charlietfl
    Nov 11 at 12:00










1




1




That sequence is not possible within beforeunload. You must determine synchronously whether to show the prompt or not
– charlietfl
Nov 11 at 11:31






That sequence is not possible within beforeunload. You must determine synchronously whether to show the prompt or not
– charlietfl
Nov 11 at 11:31






1




1




Suggest you reconsider approach and do cleanup on periodic interval and use some sort of dirty flag when user changes something in between cleanups and use that flag to show unload prompt. Also you can not use an alert() inside that event handler
– charlietfl
Nov 11 at 11:39






Suggest you reconsider approach and do cleanup on periodic interval and use some sort of dirty flag when user changes something in between cleanups and use that flag to show unload prompt. Also you can not use an alert() inside that event handler
– charlietfl
Nov 11 at 11:39






1




1




Not really. Your choices are show the prompt or not and that is about all you can control. If the ajax is critical you need to take care of it before user tries to leave
– charlietfl
Nov 11 at 11:49






Not really. Your choices are show the prompt or not and that is about all you can control. If the ajax is critical you need to take care of it before user tries to leave
– charlietfl
Nov 11 at 11:49






2




2




I know that's what you would like but it is just not that simple. What if users browser crashes or they lose power? That event and ajax would never even occur.
– charlietfl
Nov 11 at 11:57




I know that's what you would like but it is just not that simple. What if users browser crashes or they lose power? That event and ajax would never even occur.
– charlietfl
Nov 11 at 11:57




1




1




Safest approach is do server updates or localStorage updates for every change user makes then there is no cleanup needed
– charlietfl
Nov 11 at 12:00






Safest approach is do server updates or localStorage updates for every change user makes then there is no cleanup needed
– charlietfl
Nov 11 at 12:00



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248241%2fjquery-ajax-cleanup-code-before-closing-page%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248241%2fjquery-ajax-cleanup-code-before-closing-page%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine