Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Tip/Trick

How to fix JavaScript Errors

Rate me:
Please Sign up or sign in to vote.
3.65/5 (11 votes)
8 May 2011CPOL2 min read 29K   6
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Social Group (No members)


Comments and Discussions

 
GeneralReason for my vote of 4 useful about debugger keyword Pin
seaplanner11-Oct-11 5:24
seaplanner11-Oct-11 5:24 
Generalcool :) Pin
Shahriar Iqbal Chowdhury/Galib8-May-11 10:30
professionalShahriar Iqbal Chowdhury/Galib8-May-11 10:30 
GeneralNice. Pin
Shahista.Shaikh7-May-11 3:29
Shahista.Shaikh7-May-11 3:29 
GeneralRe: then you can rate me Pin
Steven.Pinto20009-May-11 1:09
Steven.Pinto20009-May-11 1:09 
GeneralWill be handy! :thumbsup: Pin
Sandeep Mewara6-May-11 19:50
mveSandeep Mewara6-May-11 19:50 
GeneralRe: Thank you Pin
Steven.Pinto20006-May-11 20:04
Steven.Pinto20006-May-11 20:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.