简体   繁体   English

关闭彩盒模式窗口时如何防止退出时出现javascript警告?

[英]How to prevent javascript alert on exit when closing colorbox modal window?

I have a javascript message alert the user when he attempts to leave a form page in case he hasn't finished filling out the form as follows: 我有一个javascript消息提醒用户,当他试图离开表单页面时,如果他还没有填写表格,如下所示:

window.onbeforeunload = function (evt) {
if(window.nounloadcheck == true) return;
var message = 'Data you entered may not be saved';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}

I also have a link on the same page that opens a colorbox modal so user can upload a new photo. 我在同一页面上也有一个链接,用于打开彩色模块,以便用户可以上传新照片。 Upon submission of new photo, the colorbox modal closes and refreshes the main page so the user can see the new photo they uploaded. 提交新照片后,颜色框模式将关闭并刷新主页面,以便用户可以看到他们上传的新照片。 This is the colorbox code for refreshing the main page: 这是用于刷新主页面的颜色框代码:

jQuery(document).ready(function(){
jQuery(".iframe").colorbox({iframe:true, width:"748px", height:"92%", onClosed:function(){ location.reload(true); } });
});

The problem is when the page attempts to reload, the javascript alert comes up. 问题是当页面尝试重新加载时,会出现javascript警报。 I've tried: 我试过了:

 <form ... onSubmit='window.nounloadcheck = true; return true;'>

This words for the submit button for the form on the main page. 这个单词用于主页面上表单的提交按钮。 But it doesn't work for the submit button on the colorbox modal. 但它不适用于colorbox模式上的提交按钮。 How would I prevent the main page from showing javascript alert upon closing of the colorbox modal? 在关闭彩盒模式时,如何阻止主页显示javascript警告?

你不能在location.reload之前将它添加到onClosed函数吗?

You are looking for: 您正在寻找:

<form ... onSubmit='window.onbeforeunload = null; return true;'>

This will clear out the event handler before it gets run. 这将在事件处理程序运行之前清除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM