onbeforeunload对话框取消与window.location.href IE8错误

对于这个不起眼的标题感到抱歉,希望我能解释一下:

我有一个标准,“你确定要离开”对话框会在用户试图离开页面时弹出:

window.onbeforeunload = function() { return 'You have unsaved changes'; } 

当这与window.location.href 并且用户单击取消时 ,Internet Explorer 8或更早版本中将引发“未指定的错误”。 其他现代浏览器似乎也没有发生同样的情况。

  $('input').click(function() { window.location.href = 'http://www.google.com'; // error is thrown here });   

有什么办法可以继续使用window.location.href并在IE8中解决这个bug吗?

这是一个已知的IE漏洞。 我想你可以使用try block捕获该错误。