views:

716

answers:

7

Hi, 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.

A: 

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

On Firefox, go to

http://getfirebug.com

to get it.

動靜能量
A: 

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.

Dan Herbert
+7  A: 

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/

Slace
Thanks for that, this link is really helpful!The question is if I have a lot of code without try/catch and it's just a plain js error, is there any easy way of knowing what happend? getting stack trace? 10x
Tal
The best way would be attach your own function to window.onerror event. This event is raised when an unhandled exception happens. The link then shows how to write a stacktracer completely. My JS book is currently at work so I can't write more of an example myself
Slace
thanks,attaching to this event will cause all js errors to trigger the function, can't this have performance implications?Is it possible to have only a portion of the code monitored for exceptions?
Tal
No, window.onerror will listen for any unhandled exception. Your only way to achieve it is to try/ catch your method calls. You may have performance impacts with that event, but I'm not sure, your best option then is to only do it in your debug JS file(s)
Slace
+1  A: 

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.

Tom
+1 for a tool which works in IE, -1 for recommending this *over* firebug
annakata
nice tool, however it will add this firebug lite js to the response which will add to networking...
Tal
A: 

This will print you a stack trace:

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

print( Stack() );
Artem Barger
+1  A: 

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

Amr ElGarhy
A: 

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.

gzzzur