0

I have read that if I return something from the beforeunload handler, the browser will show a yes/no dialog with this prompt. How can this be done conditionally? For example, if a user has some unsaved data then ask him and if not then don't ask.

Community
  • 1
  • 1
Dims
  • 47,675
  • 117
  • 331
  • 600
  • 1
    possible duplicate of [How to display onbeforeunload dialog when appropriate?](http://stackoverflow.com/questions/2663728/how-to-display-onbeforeunload-dialog-when-appropriate) – JJJ Feb 07 '12 at 10:07

1 Answers1

0

try this. not tested:

$(window).unload(function() {
     if(something){
       if(confirm("Do you want to save before leave the page")){
           return false;
       }
    }

});
Yorgo
  • 2,668
  • 1
  • 16
  • 24
  • 1
    Won't work: you can't replace the beforeunload prompt with your own. Returning `false` won't stop the browser from moving away from the page. – JJJ Feb 07 '12 at 11:34
  • It is said in manual that `unload()` can't prevent the closing, only `beforeunload` can. – Dims Feb 07 '12 at 16:58