我一直在锤击这个问题,但一无所获。
我有一个取消按钮,执行“window.location = '404.htm';”当点击时。 onbeforeunload 处理程序会触发两次,但前提是用户在第一个对话框中单击“留在此页面”。正常的导航(页面刷新、转到主页) onbeforeunload 处理程序仅触发一次。
我成功地用最少的 HTML 和脚本重现了该行为:
<!DOCTYPE html>
<html>
<head>
<title>Test page</title>
</head>
<body onbeforeunload="return 'Unsaved changes';">
<form name="form1" id="form1">
<input id='cmdCancel' type="button" value="Cancel" onclick="window.location = '404.htm';" />
</form>
<script type="text/javascript">
/*
* Connecting the events via code makes no difference
*
document.forms[0].cmdCancel.onclick =
function () {
alert('Clicked cancel. About to navigate...');
window.location = '404.htm';
};
window.onbeforeunload = function () {
return "Unsaved changes";
};
*/
</script>
</body>
</html>
有很多“onbeforeunload 调用两次”的点击,但没有最小的复制,也没有实际的解决方案。
有什么建议么?
Thanks!
试试这个,这对我有用。该解决方案最初由 Amit 发布于http://social.msdn.microsoft.com/Forums/eu/sharepointinfopath/thread/13000cd8-5c50-4260-a0d2-bc404764966d?prof=required http://social.msdn.microsoft.com/Forums/eu/sharepointinfopath/thread/13000cd8-5c50-4260-a0d2-bc404764966d?prof=required
请记住,暂时禁用 onbeforeunload 并在一段时间后重新启用它是关键。
var checkChangesEnabled = true;
window.onbeforeunload = confirmExit;
function confirmExit()
{
if(checkChangesEnabled)
{
event.returnValue = "You have not saved your document yet.";
disableCheck();
}
}
function disableCheck()
{
checkChangesEnabled = false;
setTimeout("enableCheck()", "100");
}
function enableCheck()
{
checkChangesEnabled = true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)