onbeforeunload dialog cancellation with window.location.href IE8 bug

痴心易碎 提交于 2019-12-07 00:37:56

问题


Sorry about the obscure title, hopefully I can explain:

I have a standard, "Are you sure you wish to leave" dialog that pops up when a user tries to leave a page:

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

When this is coupled with window.location.href and the user clicking cancel an 'Unspecified error' is thrown in internet explorer 8 or earlier. The same doesn't seem to happen with other modern browsers.

<script type="text/javascript">
    $('input').click(function() {
        window.location.href = 'http://www.google.com'; // error is thrown here
    });
</script>

<input type="button" value="Go to google" />

Is there any way I can continue to use window.location.href and get around this bug in IE8?


回答1:


It's a known IE bugs. I think you can just catch that error use try block.



来源:https://stackoverflow.com/questions/8264566/onbeforeunload-dialog-cancellation-with-window-location-href-ie8-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!