简体   繁体   中英

Debugging Javascript in Visual Studio (or other JS debugger)

I have a .js file that normally gets executed by cscript.exe (ie is not run in the browser and cannot be run there).

I know, that I can feed cscript.exe the //X parameter in order to get asked for a debugger to choose. Ok. That's fine.

I select "Visual Studio 2005 Debugger", IDE comes up, execution stops on the first line. Fine.

Script terminates (or I terminate it), I edit something and want to debug it again.

Simple thought would be just to hit F5 and run the debugger again. But this doesn't work. VS just tells me that it couldn't find any debugging symbols in cscript.exe : 屏幕截图

What now? Starting cscript.exe from the command line again for each debug run is quite cumbersome in my opinion.

Is there a way to simply debug the script with VS? Also hints for other debugging tools would be appreciated.


Edit:
The answer of t0nyh0 is pretty close. I create a dummy console application, compile and the debugger comes up. However, two things are not perfect

  1. cscript.exe always asks for the debugger to use (which instance of VS).
    Could this be overcome by specifying a certain debugger instance directly in the command line?

  2. In order to fire a post-build event, there have to be some modifications in the sources. Currently, I simply add/delete a blank line to trigger building of my dummy project.
    Is there a way to always execute the post-build script, even if nothing has changed?

There might not be a way to attach the debugger to cscript.exe itself, but you may be able to create a post-build event that runs a batch file that executes the cscript.exe //x myScript.js command so that every time you build, it executes for you automatically.

See this for more information on post-build events: http://msdn.microsoft.com/en-us/library/ke5z92ks(v=vs.80).aspx

While not a debugger tool, you should consider using a JavaScript testing framework for Visual Studio, such as Chutzpah , which will most likely make your life a lot easier.

Along with standard browser debugging tools (Firebug or Chrome Inspector), I've found that's all I usually need to build clean, tested, mostly-bug-free JavaScript code.

I don't have cscript at hand, but I think you can try to attach VS to the process manually.

After you start up your js using cscript.exe //x myScript.js , click "Debug - Attach to Process", find your cscript.exe process and attach to it.

I forget if VS2005 has this function but VS2008 and VS2010 do.

It is the native debug action that VS takes to debug (attach to the running process). If this doesn't work, I don't think you can do this using Visual Studio.

Best Javascript Debugger is Rhino Debugger See http://www.mozilla.org/rhino/debugger.html . it is open source and you can get the source code of the Debugger GUI. you can customise it as you wish :-) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM