Javascript 如何覆盖onbeforeunload函数的默认弹出框
我想自定义默认框以显示我自己的fancybox表单,使客户订阅我的网站 在下面的代码中,首先显示默认对话框,然后显示我的表单。但我想先展示我的fancybox。我试着像下面那样。但我无法实现我预期的产出。我怎样才能解决这个问题Javascript 如何覆盖onbeforeunload函数的默认弹出框,javascript,jquery,Javascript,Jquery,我想自定义默认框以显示我自己的fancybox表单,使客户订阅我的网站 在下面的代码中,首先显示默认对话框,然后显示我的表单。但我想先展示我的fancybox。我试着像下面那样。但我无法实现我预期的产出。我怎样才能解决这个问题 $(document).ready(function () { // Shadow box initialization Shadowbox.init({ handleOversize: "resize", overlayO
$(document).ready(function () {
// Shadow box initialization
Shadowbox.init({
handleOversize: "resize",
overlayOpacity: 0.9
});
// This makes sure that the pop up isn't shown
// if the user is navigating within the website
$(window).click(function () {
window.onbeforeunload = null;
});
window.onbeforeunload = function (e) {
//console.log(Event);
//e.preventDefault();
//e.defaultPrevented();
//open thickbox
Shadowbox.open({
content: '<div id="welcome-msg">Welcome to my website!</div>',
player: "html",
width: 540,
height: 380
});
return 'You have unsaved changes!';
}
});
如果您只是返回函数,它将弹出默认的确认框 你所要做的就是弄清楚什么时候你希望你的“花式盒子”被默认的确认框取代,然后返回消息 因此,不要直接返回,而是等待用户执行subscribe/notsubscribe操作,然后返回消息 也许您可以创建一个全局bool,并仅在bool为true时在beforeunload事件中返回消息
如果你不能或不想检测他们何时做出动作,你可以设置一个计时器;这完全取决于您。是否有其他可定制的jquery插件?