window.onbeforeunload = function() { return 'Are you sure that you want to leave this page?'; };
这工作正常.但我的问题是当用户尝试使用提交按钮确认框提交表单时将出现.我不想询问用户是否提交表格,否则我想要确认.这怎么可能?
var userSubmitted=false; $('form').submit(function() { userSubmitted = true; });
那么检查一下
window.onbeforeunload = function() { if(!userSubmitted) return 'Are you sure that you want to leave this page?'; };
PS:交叉检查onbeforunload兼容性的跨浏览器.