javascript – 当window.location在按钮事件中更改时,IE9 onbeforeunload调用两次…最小再现
作者:互联网
我一直在锤击和锤击,只是无法到达任何地方.
我有一个取消按钮,它执行“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两次调用”有很多点击,但没有最小的再现,也没有实际的解决方案.
有什么建议?
谢谢!
解决方法:
试试这个,这对我有用.此解决方案最初由Amit发布
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;
}
标签:javascript,internet-explorer-9,onbeforeunload,html 来源: https://codeday.me/bug/20190630/1334350.html