How can we listen for errors that do not trigger window.onerror?
window.onerror
is triggered whether it was a syntax or runtime error. This page from quirksmode has lists of what error events it will and will not catch.
Could you please provide a small code example to show how we can listen for such errors? Can we listen for SyntaxError too?
For a small code example to show how we can listen for such errors:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
window.onerror = function (errorMsg, url, lineNumber) {
alert(errorMsg + lineNumber);
// alert("This is a stack trace! Wow! --> %s", error.stack);
};
</script>
</head>
<body>
<script type="text/javascript">
//var x=document.getElementById("demo").value; //uncomment and run to see
document.write('careless to close the parentheses?'; // ')' is not given
</script>
</body>
</html>
running this example in your browser will pop up an alert message similar to this:
JavaScript error: SyntaxError: missing ) after argument list on line 26 for page_url
In the above example: window.onerror = function(message, url, linenumber)
, the arguments are:
message
: the error message (DOMString)url
: the URL of the file containing the error (DOMString)linenumber
: the line number where the error occurred (unsigned long)
If you run the same example by putting var x=document.getElementById("demo").value;
instead of the code with syntax error(as i have shown in the example), it will also be caught by the window.onerror()
function and will show an alert message similar to this:
JavaScript error: TypeError: document.getElementById(...) is null on line 25 for page_url
window.onerror acts something like a global try/catch block, allowing you to gracefully handle(even with server logging) uncaught exceptions you didn’t expect to see:
-
uncaught exceptions
throw "some messages"
call_something_undefined()
;cross_origin_iframe.contentWindow.document;
, a security exception
-
some more compile error
<script>{</script>
<script>for(;)</script>
<script>"oops</script>
setTimeout("{", 10);
, it will attempt to compile the first argument as a script
But two major issues described here nicely:
-
Unlike a local try/catch block, the window.onerror handler doesn’t have direct access to the exception object, and is executed in the global context rather than locally where the error occurred. That means that developers don’t have access to a call stack, and can’t build a call stack themselves by walking up the chain of a method’s callers.
-
Browsers go to great lengths to sanitize the data provided to the handler in order to prevent unintentional data leakage from cross-origin scripts. If you host your JavaScript on a CDN (as you ought), you’ll get “Script error.”, “”, and 0 in the above handler. That’s not particularly helpful.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2016-04-12 javascript 中获取对象的长度(map对象的长度)--js关联数组的长度