views:

753

answers:

5

I have a jsp with lots of javascript code. Whenever there is a javascript error on the page, shown in the status bar of the IE browser, the line number reported to contain the error, does not match with the line number that actually contains the error. I am doing a right click>view source to find the line number reported. But that line does not contain the error. The error, I assume, is in some other line. What could be the reason for the erroneous line numbers being reported.

Please Help.

A: 

If you want to get exact reason and location of error and if you want to perform extensive javascript debugging I would recommend you using FireBug.

Darin Dimitrov
This is sometimes not possible since different errors often appear in IE than FF (especially those not related to syntax).
Justin Johnson
+1  A: 

Internet Explorer is awful at reporting Javascript line numbers - usually the line number reported is where the Javascript <script> tag started in the HTML file, instead of the location line number in the Javascript file. Only rely on the 'Error Reported', the Line number isn't worth anything with IE.

Use another browser, for example Firefox with the Firebug Extension installed, or Google Chrome which has it's built in Web Inspector which is also great.

Jimmie Lin
A: 

Here is a nice article

Debugging JavaScript: Understanding JavaScript Error Messages

The line number, in particular, turns out to be a lot less helpful than you might expect. Browsers differ in their determination of the line number and thus do not reliably report the correct line number that an error occurred at in relation to the source code. Internet Explorer, for instance, reports the line number in relation to the browser's own internal rendering of the document source, which may or may not match the source file! Firefox reports the location of the error more reliably, reporting the script file that an error occurred in where applicable. Firefox will not however provide you with details about the element that caused the error, known as the "caller". This information, which can be useful in quickly tracing the cause of an error, is currently only provided by Internet Explorer.

rahul
+1  A: 

As noted in other answers, IE is bad at reporting line numbers for errors. However, the built-in debugger in IE 8 is much more helpful, so I suggest you try that.

Tim Down
I agree. "Tools -> Developer Tools" is what you want instead of "View Source"Also if you change your internet options to stop/prompt on javascript errors, it usually comes up with a "do you want to debug?" button which takes you straight in to the dev tools
Graza
+1  A: 

I found the problem after a lot of trials. Hopefully, this will be of use to some guys facing this frustrating problem.

RightClick>View Source is the code what the browser sees to render the page. However, that is not all. The page could also have other HTTP requests to css ans js files. That was what was happening in my case. The error was in an imported(NOT INCLUDED ; had it been included the error would have been on the code) js file. And the line number reported was relative to that js file .

In retrospect ,however, i find this to be correct, since the imported files are stored separately in the browser cache, as they are independent HTTP requests to the web server. And hence should not appear in the RightClick>View Source code.

POTENTIAL PROBLEM: However, though in my case the line number in the individual js file,reported, was found to be correct, that may not always be true. In most enterprise applications, js and css es are often minified to reduce the byte footprint. Hence the js file that you may be looking at , in your IDE, will not be the same as the browser sees. Hence the line numbers could then be different. The line numbers will be w.r.t the compressed version of the file that the browser sees.

IE JAVASCRIPT DEBUGGING: I found this page , which promises to offer debugger environment(albeit ,not sophiscticated) in the IE environment.

http://www.jonathanboutelle.com/mt/archives/2006/01/howto_debug_jav.html I didnt try it, as I dont have the complete Office package installed.

The Machine