当按钮事件中window.location更改时,IE9 onbeforeunload调用两次...最小限度复制 [英] IE9 onbeforeunload called twice when window.location changed in button event... minimal reproduction

查看:112
本文介绍了当按钮事件中window.location更改时,IE9 onbeforeunload调用两次...最小限度复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个取消按钮来执行window.location ='404热媒;点击时。 onbeforeunload处理程序触发两次,但只有当用户点击保留在此页面上到第一个对话框。正常的导航(页面刷新,进入主页)onbeforeunload处理程序只触发一次。



我已经设法使用最少量的HTML和脚本重新编写行为:

 <!DOCTYPE html> 
< html>
< head>
< title>测试页面< /标题>
< / head>
< body onbeforeunload =return未保存的更改;>
< form name =form1id =form1>
< input id ='cmdCancel'type =buttonvalue =Cancelonclick =window.location ='404.htm'; />
< / form>
< script type =text / javascript>
/ *
*通过代码连接事件没有区别
*
document.forms [0] .cmdCancel.onclick =
function(){
提醒('点击取消。关于导航...');
window.location ='404.htm';
};

window.onbeforeunload = function(){
返回未保存的更改;
};

* /
< / script>
< / body>
< / html>

onbeforeunload被称为两次的命中次数很多,但没有最小的再现,也没有实际的解决方案。 / p>

有什么建议吗?



谢谢!

解决方案

试试这个,这对我有用。此解决方案最初由Amit发布在
http://social.msdn.microsoft.com/Forums/eu/sharepointinfopath/thread/13000cd8-5c50-4260-a0d2-bc404764966d?prof=required



请记住,临时禁用onbeforeunload并在短时间内启用它是关键。

  var checkChangesEnabled = true; 

window.onbeforeunload = confirmExit;

函数confirmExit()
{
if(checkChangesEnabled)
{
event.returnValue =您尚未保存文档。
disableCheck();



function disableCheck()
{
checkChangesEnabled = false;
setTimeout(enableCheck(),100);


function enableCheck()
{
checkChangesEnabled = true;
}


I've been hammering and hammering at this and just can't get anywhere.

I have a cancel button that does a "window.location = '404.htm';" when clicked. The onbeforeunload handler fires twice, but only if the user clicks "Stay on this page" to the first dialog. A normal navigation (page refresh, going to home page) the onbeforeunload handler only fires once.

I've managed to repro the behavior with a minimal amount of HTML and script:

<!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>

There are lots of hits for "onbeforeunload called twice" but no minimal reproduction and no actual solutions.

Any suggestions?

Thanks!

解决方案

Try this, this worked for me. This solution was originally posted by Amit at http://social.msdn.microsoft.com/Forums/eu/sharepointinfopath/thread/13000cd8-5c50-4260-a0d2-bc404764966d?prof=required

Remember, disabling the onbeforeunload temporarily and enabling it back after a short while is the key.

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;
}

这篇关于当按钮事件中window.location更改时,IE9 onbeforeunload调用两次...最小限度复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆