Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent a page unload with jQuery?

In my program, if a user tries to leave a page, he'll receive a dialog box asking if he is sure he wants to leave.

How should I implement the 'cancel' option if the user chooses not to leave the page?

Source javascript code:

$(window).unload(function(){
   var c= confirm ("Are you sure?");
   if (c){
       alert("Thanks. Good luck!");
   }
   else{
       ????
   }
});
like image 736
tatiana_c Avatar asked Dec 05 '22 17:12

tatiana_c


1 Answers

window.onbeforeunload = function() {
    return 'You have unsaved changes!';
}

many question about this in stackoverflow How can I override the OnBeforeUnload dialog and replace it with my own?

JavaScript + onbeforeunload

like image 75
sandeep Avatar answered Dec 27 '22 17:12

sandeep