<!doctype html> <html> <head> <meta charset="utf-8"> <title>使用window.onerror捕获错误信息</title> <script type="text/javascript"> /** * @param string errorMessage 错误信息 * @param string scriptURI 出错的文件 * @param long lineNumber 出错代码的行号 * @param long columnNumber 出错代码的列号 * @param object errorObj 错误的详细信息,Anything */ window.onerror = function (errorMessage, scriptURI, lineNumber, columnNumber, errorObj) { console.log('错误信息:', errorMessage); console.log('出错文件:', scriptURI); console.log('出错行号:', lineNumber); console.log('出错列号:', columnNumber); console.log('错误详情:', errorObj); } demo(); // 调用未定义的函数 // 错误信息: Uncaught ReferenceError: demo is not defined // 出错文件: file:///C:/Users/Developer/Desktop/demo.html // 出错行号: 22 // 出错列号: 1 // 错误详情: ReferenceError: demo is not defined // at demo.html:22:1 </script> </head> <body> </body> </html>
Copyright © 2023 码农人生. All Rights Reserved