關於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 联系我们: