Why does IE11 throw an error when using window.onbeforeunload?

I violated my problem until the following simple code:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>IE test</title> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <script type="text/javascript" charset="utf-8"> $( document ).ready(function() { $('.js-click').click(function(e){ window.location.href = 'http://www.google.com/'; }); window.onbeforeunload = function(e){ return 'moving on'; }; }); </script> </head> <body> <a href="#" class="js-click">Google</a> </body> </html> 

This works as expected in chrome without warning or error, but in IE11 it throws the following error when you select "Stay on this Page":

 File: 10.0.1.126:8080, Line: 10, Column: 11 

Any idea why?

+5
source share
1 answer

In fact, the error arises from:

 window.location.href = 'http://www.google.com/'; 

And this is just an assumption, but I believe that IE developers wanted to catch it when the user decides not to follow the link. This way you can try to catch this block and you will find out when the user is not redirected (as a result of onbeforeupload).

 try { window.location.href = 'http://www.google.com'; } catch (error) { alert("YU NO REDIRECT"); } 

If you are console.log(error) , you will see that there is no error message, and the error number is -2147467259.

+6
source

All Articles