我正在创建一个测试应用程序,向用户提出一些问题。在测验过程中,如果用户选择退出测验,我会发出警报,指出所有更改都将丢失,并且将提交测试。我是通过以下方式完成的
HTML
<form id="testsForm" method="POST" action="/tests/submit">
<!--test questions here-->
</form>
jQuery
var submitted = false;saveBeforeUnload = false;
window.onbeforeunload = function () {
if (!submitted) {
saveBeforeUnload=true;
return "You are about to leave this order form. You will lose any information...";
}
}//alert given to the user to confirm if he/she wishes to leave the form
setInterval(function() {
if (saveBeforeUnload) {
saveBeforeUnload = false;
$("#testsForm").submit();
}
}, 500);//form submit logic
此逻辑在Firefox上有效,但在Safari和Chrome上无效。请帮帮我