The Art of Debugging – A Developer’s Best Friend – Lesson 12 – Debugging Javascript In Web Pages - Repost

The first step is to enable debugging within the browser.

clip_image002

Be sure to allow for script debugging. To do this, start Internet explorer and select from the tools menu. Click on options. Navigate to the advanced tab and make sure the checkboxes in the red box art deselected.

clip_image004

Within the Visual Studio Solution Explorer, make sure that your web project is the startup project.

clip_image006

Once you have made your web project the startup project, now you will define which web page is the start page once you start the debugger.

clip_image008

Start the debugging process by selecting step into, which is the F11 key.

You may be asked to modify the configuration file to enable debugging. You should obviously select yes.

clip_image010

Notice that the instruction pointer has begun running the JavaScript. That this did you can easily single step or step over the code.

clip_image012

The various windows that we discussed previously can be used.

clip_image014

You can see in the JavaScript code above the value of the mydate variable.

Method #2 – To Debug Javascript

The next approach to debugging launches the debugger directly from within Internet explorer. The way this is accomplished is by using the view menu once the web page is loaded. From the View menu select View Script Debugger.

In this example we will select Break At Next Statement.

clip_image016

Your web page will now sit and wait state until JavaScript gets executed. In this case, clicking on the button will initiate some JavaScript. At that point we will be prompted with the opportunity to choose our debugger. We will choose Visual Studio.

clip_image018

Choose yes in the dialog box below.

clip_image020

At this stage we are asked which debugger we wish to use. Since visual studio is the most powerful debugger, we will select Visual Studio. We will select new instance of Visual Studio 2008, since we do not want to interfere with the existing instance of visual studio already open which is running the solution called CSBreakPoints.

clip_image022

Notice that the bug are brought us appropriately to the onclick event.

clip_image024

Step into the code with F11 key. This technique will cover most of the debugging scenarios that you encounter.

clip_image026

We hit the F5 or continue debugging command to finish running the JavaScript. You can see the result below were appropriately.

clip_image028