最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I prevent a page unload with jQuery? - Stack Overflow

programmeradmin0浏览0评论

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{
       ????
   }
});

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{
       ????
   }
});
Share Improve this question edited Aug 21, 2019 at 8:11 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Mar 5, 2012 at 9:56 tatiana_ctatiana_c 9587 gold badges16 silver badges31 bronze badges 2
  • 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 Commented Mar 5, 2012 at 9:58
  • Thanks, I am agree. It is a very good advice when I think about it now. – tatiana_c Commented Mar 5, 2012 at 10:12
Add a comment  | 

2 Answers 2

Reset to default 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

 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');?>";
        }
    }

 });
发布评论

评论列表(0)

  1. 暂无评论