onbeforeunload dialog cancellation with [removed].

2019-04-25 02:28发布

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条回答
爷、活的狠高调
2楼-- · 2019-04-25 03:28

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

查看更多
登录 后发表回答