Javascript 为什么IE11在使用window.onbeforeunload时抛出错误?
我将问题分解为以下简单代码:Javascript 为什么IE11在使用window.onbeforeunload时抛出错误?,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,我将问题分解为以下简单代码: <!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/javasc
<!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>
知道原因吗?实际上错误来自:
window.location.href = 'http://www.google.com/';
这只是猜测,但我相信IE的开发者希望能够抓住用户决定不关注链接的时机。因此,您实际上可以尝试捕获此块,您将知道用户何时没有被重定向(由于onbeforeupload)
如果您
console.log(error)
您将看到没有错误消息,错误号是-2147467259。在IE 11.0.9600.17842中对我很好,我正在运行11.0.9600.17843。它实际上并没有抛出一个错误对话框,但我在JavaScript控制台中看到了这条消息。你有没有碰巧看看那里?
window.location.href = 'http://www.google.com/';
try {
window.location.href = 'http://www.google.com';
} catch (error) {
alert("Y U NO REDIRECT");
}