关于javascript错误,如何识别方法或js文件的问题?

[英]on a javascript error, how to identify method or js file with the problem?


When a javascript error occures in IE (or in other browsers) you get a popup saying that javascript error has occurred - usually this comes with a line number and some hint. Sometimes it comes with line 0 and with no way of knowing what the problem is.

当IE(或其他浏览器)中出现javascript错误时,您会收到一个弹出窗口,指出已发生javascript错误 - 通常会附带行号和一些提示。有时它会出现0行,无法知道问题所在。

Javscript can come from HTML itself, from a js file or from JSP (and more). Microsoft has a script debugger that helps a lot in finding where js errors are, however sometimes when a js error occurs the script debugger cannot find the code portion and thus its difficult of finding where is the root cause of the problem.

Javscript可以来自HTML本身,来自js文件或JSP(以及更多)。 Microsoft有一个脚本调试器,可以帮助找到js错误的位置,但有时当js错误发生时,脚本调试器无法找到代码部分,因此很难找到问题的根本原因。

My question is whether anyone knows any way of making script debugger find the code any way (mostly happen with js code that is in JSP file), or at least include in the IE popup the method or js file where the error has occurred. (it only display the line number, and many times its line 0...).

我的问题是,是否有人知道任何方式使脚本调试器以任何方式找到代码(主要发生在JSP文件中的js代码),或者至少在IE弹出窗口中包含发生错误的方法或js文件。 (它只显示行号,并且多次显示行0 ...)。

Thanks, Tal.

7 个解决方案

#1


The error object which is created when an error is thrown by JavaScript is very unreliable when it comes to the source line, especially within IE. Browsers like Firefox and Safari are better at line numbers, but they are generally pointless due to minification of the files.

当涉及到源代码行时,JavaScript抛出错误时创建的错误对象非常不可靠,特别是在IE中。像Firefox和Safari这样的浏览器在行号上更好,但由于文件的缩小,它们通常没有意义。

What is obviously of more use is getting the call stack, but due to the anonymous nature of JavaScript functions (well, that they can be anonymous) a call stack can often be hard to work out.

显然更有用的是获取调用堆栈,但由于JavaScript函数的匿名特性(好吧,它们可以是匿名的),调用堆栈通常很难解决。

If you're doing a try/ catch you can do arguments.callee which will return you the method which called the current method which failed.

如果您正在尝试/ catch,您可以执行arguments.callee,它将返回调用失败的当前方法的方法。

Here's a good example of doing a complete stack in JavaScript - http://eriwen.com/javascript/js-stack-trace/

这是一个用JavaScript完成堆栈的好例子 - http://eriwen.com/javascript/js-stack-trace/

#2


Also developer tools included with Internet Explorer 8 is something good to trace and debug your javascript code

此外,Internet Explorer 8附带的开发人员工具可以很好地跟踪和调试您的JavaScript代码

#3


There is a version of Firebug called Firebug Lite that will work with Internet Explorer. It's performance is going to be based on how complex your pages are; however, for relatively lightweight pages, it should provide some insight.

有一个名为Firebug Lite的Firebug版本可以与Internet Explorer配合使用。它的性能将基于您的页面的复杂程度;但是,对于相对轻量级的页面,它应该提供一些见解。

I recommend this tool rather than simply using Firebug and Firefox because not all errors that occur in Internet Explorer will occur in Firefox, and so performing any debugging in that browser may not yield any results.

我建议使用此工具而不是简单地使用Firebug和Firefox,因为并非Internet Explorer中出现的所有错误都会在Firefox中发生,因此在该浏览器中执行任何调试都不会产生任何结果。

#4


Firebug on Firefox is usually considered one of the best debugging tools.

Firefox上的Firebug通常被认为是最好的调试工具之一。

On Firefox, go to

在Firefox上,转到

http://getfirebug.com

to get it.

为拿到它,为实现它。

#5


This will print you a stack trace:

这将打印出一个堆栈跟踪:

function Stack() 
{
  try 
     { 
          throw Error() 
     } 
  catch(ex) 
  { 
     return ex.stack 
  } 
};

print( Stack() );

#6


If all else fails (and when dealing with IE it sometimes does) you can always walk through your code with alerts. It's crude and tedious, but sometimes it's all you can do: Simply:

如果所有其他方法都失败了(并且有时会处理IE),您可以随时通过警报遍历代码。这是粗暴乏味的,但有时你只能这样做:简单地说:

var count = 0;

then sprinkle some:

然后洒一些:

alert(count++);

at strategic lines along your code and note where it stops alerting.

在您的代码的战略线上,并注意它停止警报的位置。

Lather rinse repeat until you have your line.

泡沫重复冲洗,直到你的线。

#7


If using Firefox you can press Ctrl + Shift + J to bring up the JavaScript error console that is built into Firefox, which will tell you exactly what went wrong.

如果使用Firefox,您可以按Ctrl + Shift + J打开Firefox内置的JavaScript错误控制台,它会告诉您到底出了什么问题。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/09/c008c1f15049b1572990d67cb07e7500.html



 
  © 2014-2022 ITdaan.com 联系我们: