gpt4 book ai didi

javascript - 为什么IE11在使用window.onbeforeunload时会报错?

转载 作者:数据小太阳 更新时间:2023-10-29 06:11:03 25 4
gpt4 key购买 nike

我已将我的问题分解为以下简单代码:

<!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>

这在 chrome 中按预期工作,没有警告或错误,但在 IE11 中,当您选择“停留在此页面”时,它会抛出以下错误:

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

知道为什么吗?

最佳答案

其实错误来自:

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

这只是推测,但我相信 IE 的开发人员希望能够在用户决定不点击链接时捕捉到。因此,您实际上可以 try catch 此 block ,并且您会知道用户何时没有被重定向(作为 onbeforeupload 的结果)。

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

如果您console.log(error),您会看到没有错误消息,错误号是-2147467259。

关于javascript - 为什么IE11在使用window.onbeforeunload时会报错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31198669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com