Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onbeforeunload dialog cancellation with window.location.href IE8 bug

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?

like image 916
ajbeaven Avatar asked Nov 25 '11 03:11

ajbeaven


1 Answers

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

like image 161
pnuts Avatar answered Nov 15 '22 03:11

pnuts