How can I prevent a page unload with jQuery?

南楼画角 提交于 2019-12-23 14:59:05

问题


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

回答1:


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




回答2:


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

 });


来源:https://stackoverflow.com/questions/9564663/how-can-i-prevent-a-page-unload-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!