最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

debugging - on a javascript error, how to identify method or js file with the problem? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

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...).

Thanks, Tal.

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.

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.

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...).

Thanks, Tal.

Share Improve this question asked Jun 9, 2009 at 11:38 TalTal 1,7934 gold badges18 silver badges20 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 8

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.

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.

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.

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

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

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.

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 on Firefox is usually considered one of the best debugging tools.

On Firefox, go to

http://getfirebug.com

to get it.

This will print you a stack trace:

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

print( Stack() );

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:

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.

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.

发布评论

评论列表(0)

  1. 暂无评论