我使用以下示例来防止用户无意中关闭其标签页或窗口。问题是它会在我的表单提交时继续提示。
window.onbeforeunload = function (e) {
var e = e || window.event;
//IE & Firefox
if (e) {
e.returnValue = 'Are you sure?';
}
// For Safari
return 'Are you sure?';
};
https://developer.mozilla.org/en/DOM/window.onbeforeunload
以下表格:
<script language='javascript'>
window.onbeforeunload = function (e)
{
var e = e || window.event;
//IE & Firefox
if (e)
{
e.returnValue = 'Sure?';
}
// For Safari
return 'Sure?';
};
function checkForm(theForm)
{
if (mytext!='anytext')
{
window.nounloadcheck = false;
return false;
}
}
</script>
<form action='$_SERVER[PHP_SELF]' method='post' onSubmit='window.nounloadcheck = true; return checkForm(this)'>
<input type='text' name='mytext' value='anytext'>
<input type='submit' name='submit_data' value='Submit Answers' />
</form>
这是显示问题的fiddle。