简体   繁体   English

如何在JavaScript中调试客户端错误?

[英]How do I debug client errors in JavaScript?

I have a window.onerror handler that sends JavaScript errors to our server. 我有一个window.onerror处理程序,它将JavaScript错误发送到我们的服务器。 I was hoping to analyse these by using source maps to link errors in the minified source back to the original code, but it seems Chrome and Firefox only provide line numbers on error. 我希望通过使用源映射将缩小的源中的错误链接回原始代码来分析这些错误,但是Chrome和Firefox似乎仅提供错误行号。 As far as I can see, source maps are not useful without a column number. 据我所知,没有列号就没有源映射。

So, given a line number of minified source and an error message of the form x is undefined , what steps can I take to debug? 因此,给定最小行的行号,并且x is undefined格式的错误消息,我可以采取哪些步骤进行调试?

(If it makes any difference, I'm using the asset pipeline in Rails 3.2). (如果有什么不同,我将在Rails 3.2中使用资产管道)。

Chrome (and IE10) will now give you the column number as the fourth argument in the window.onerror callback. Chrome(和IE10)现在将为您提供列号,作为window.onerror回调中的第四个参数。 Here's the Firefox bug for adding column numbers , but it doesn't look too promising. 这是Firefox的用于添加列号的bug ,但看起来不太理想

If you want to use Source Maps, you'll need to download Chrome Canary (ie the pre-release dev version of Chrome). 如果要使用源地图,则需要下载Chrome Canary(即Chrome的预发行版)。

It's an experimental feature and isn't in the main Chrome release yet. 这是一项实验性功能,尚不在主要的Chrome版本中。 (even in Canary it has to be switched on explicitly in the browser options). (即使在Canary中,也必须在浏览器选项中将其显式打开)。

I recommend using firebug with firefox, here's a tutorial to get you started: 我建议将firebug与firefox结合使用,这是一个入门指南:

http://thecodecentral.com/2007/08/01/debug-javascript-with-firebug http://thecodecentral.com/2007/08/01/debug-javascript-with-firebug

I've used this a few times to debug really bad JS, however I usually just do this: 我已经多次使用它来调试非常糟糕的JS,但是我通常只是这样做:

Firefox drop down > Web developer > Web console. Firefox下拉菜单> Web开发人员> Web控制台。 This will show you the line # and the error being thrown (most important), which I find is typically sufficient without going through the debugging effort. 这将向您显示行号和引发的错误(最重要的错误),我发现通常无需进行调试即可就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM