Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onbeforeunload - after user accepts confirmation box, javascript doesn't execute

Tags:

javascript

I have the following code in onbeforeunload function.

window.onbeforeunload = function(){
   if(ThereIsUnsavedWork)
   {
      return "Please save your work so it doesn't get lost.";
   }

   DoMoreStuff(); 
}

After the confirmation window, the DoMoreStuff() function isn't getting executed. Is there a way to make sure that javascript code executes after user accepts the confirmation window?

like image 293
dev.e.loper Avatar asked Jun 04 '26 15:06

dev.e.loper


1 Answers

Try to use onbeforeunload together with onunload as below.

window.onbeforeunload = function(){
   if(ThereIsUnsavedWork)
   {
      return "Please save your work so it doesn't get lost.";
   }
}

window.onunload = function () {
    DoMoreStuff();
}
like image 93
prageeth Avatar answered Jun 06 '26 07:06

prageeth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!