如何使用jQuery阻止页面卸载? [英] How can I prevent a page unload with jQuery?

查看:70
本文介绍了如何使用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?

来源javascript代码:

Source javascript code:

$(window).unload(function(){
   var c= confirm ("Are you sure?");
   if (c){
       alert("Thanks. Good luck!");
   }
   else{
       ????
   }
});

谢谢。

推荐答案

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

很多关于stackoverflow的问题
如何覆盖OnBeforeUnload对话框并将其替换为myBeforeUnload拥有?

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

JavaScript + onbeforeunload

这篇关于如何使用jQuery阻止页面卸载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆