5

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{
       ????
   }
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tatiana_c
  • 948
  • 7
  • 16
  • 31
  • 12
    ONLY prompt the user if there are any kind of unsaved changes etc. NEVER EVER thank them with another alert dialog etc when they leave, it's the worst possible UX case. – zatatatata Mar 05 '12 at 09:58
  • Thanks, I am agree. It is a very good advice when I think about it now. – tatiana_c Mar 05 '12 at 10:12

2 Answers2

17
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

Community
  • 1
  • 1
sandeep
  • 2,244
  • 1
  • 23
  • 38
0
 jQuery(window).on('beforeunload',function(){
    var value = navigateAway(isDirty);
    if(value=="changed")`enter code here`
    {
        if(jQuery("#saveCheck").val()=="false")
        {
            return "<?php echo JText::_('OVR_WANT_TO_LEAVE');?>";
        }
    }

 });
kleopatra
  • 51,061
  • 28
  • 99
  • 211