Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

How to fix JavaScript Errors

3.65/5 (11 votes)
8 May 2011CPOL2 min read 30.3K  
Several methods to debug JavaScript

Introduction


I have always seen people asking questions on JavaScript. If we know how to debug JavaScript like any other programming language, we don't need much help on this topic.

How to Solve the Problem


First Method


So here is the trick: .NET has given us the facility to debug JavaScript, but before that, we need to do some settings in Internet Explorer.
  • Tools → Internet Options → Advanced Tab
  • Uncheck Disable script debugging (Internet Explorer) & Uncheck Disable script debugging (Others) and restart Internet Explorer

Now in a Visual Studio Project, on the aspx page in the script blog, type the debugger keyword as shown below:
C#
function Disable(controlId) {
  debugger
      document.getElementById(controlId.id).disabled = true;
      window.open("second.htm");
}


Now open your page in Internet Explorer and try to execute the function, so if the JavaScript is called on a button click, click the button. Now you will see a pop up which would give you a Visual Studio just-in-time debugger. Now select the Visual Studio instance where you want to debug JavaScript in, and click on yes and try to solve your problem.

Second Method


Another way is to replace the debugger keyword with any word which would give a JavaScript error. Let us look at it with the same example given above.
XML
function Disable(controleid) {
  dsfsdf;
      document.getElementById(controleid.id).disabled = true;
      window.open("second.htm");
  };

Now in this case, instead of showing you the JIT debugger, it would give you the error asking "Do you want to debug this web page?", along with some error details. Click on "yes" and it will show you the JIT debugger.

Third Method


If this process is lengthy, then you can use the Developers Tool given in Internet Explorer. You will find it in
Tools → Developer Tools, or simply press F12.

Now go to the script tab. Just like you place debug pointer in Visual Studio, place a debug point on the left hand side corner on the place where you want to debug, and click on start debugging to start debugging your code.

For Firefox, install the Firebug add-on. You will see Firebug in tools, and also on the right corner of the browser.

You get developer tools for almost all browsers so now you can check and debug your JavaScript.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)