我正在尝试在生产网站上记录javascript错误.到目前为止,它与网站中包含的以下代码相得益彰:
function catcherr(errorMessage, url, line) {
var parameters = "msg=" + escape(errorMessage)
+ "&url=" + escape(url)
+ "&line=" + escape(line);
new Image().src = "/error.gif?" + parameters;
return false;
};
window.onerror = catcherr;
Run Code Online (Sandbox Code Playgroud)
我正在尝试向错误添加堆栈跟踪以获取更多信息.这基本上与以下想法一起使用,包括上面的功能:
try { i.dont.exist += 0; } // does not exist - that's the point
catch (e)
{
if (e.stack) // Firefox
{
// do some stuff
Run Code Online (Sandbox Code Playgroud)
我使用jquery,一个简单的例子:
<script type="text/javascript">
jQuery(document).ready(function() {
p.foo += 1; // this should throw an error
// do stuff
});
</script>
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我在jquery的"ready"函数中有错误时,"try {i.dont.exist …