"if debug" in JavaScript?

Is there anything in JavaScript or Visual Studio to determine if code is being used in debug mode? Something like "#if DEBUG" in C #, but for JavaScript?

+6
source share
3 answers

No.

#if / #endif are preprocessor directives in C # (and other languages) that tell the compiler to conditionally include / exclude a section of code during compilation.

JavaScript is a script language that is not precompiled, and therefore it makes no sense to have preprocessor directives like these.

+3
source

IE only has conditional compilation:

 /*@cc_on @set @version = @_jscript_version @if (@_win32) document.write("You are running 32 bit IE " + @version); @elif (@win_16) document.write("You are running 16 bit IE " + @version); @else @*/ document.write("You are running another browser or an old IE."); /*@end @*/ 

good article here

+2
source

A little late, but I needed the same thing and could not refuse until a viable solution.

I have a kind of "main" javascript file, where I have a line like:

 Site.DEBUG = false; 

Then in the code I can check this constant. Now I had to decide that during the build some kind of automation would set this for me according to the configuration of the project. Here I found fnr.exe command line tool for searching and replacing in files. This is a good utility, it would be worth checking anyway. So, at the moment I created a folder in the project directory with the name BuildScripts , I copied the fnr.exe file into it and created such a batch file.

 switch_client_debug.bat REM Params: path to folder, filename, change-DEBUG-from-this, to-this fnr.exe --cl --dir "%1" --fileMask "%2" --caseSensitive --showEncoding --find "DEBUG = %3" --replace "DEBUG = %4" 

Then I defined the appropriate pre-build events in the web project as follows:

 cd $(ProjectDir)BuildScripts call switch_client_debug.bat $(ProjectDir)ts site.ts false true 

and its pair in Release config:

 cd $(ProjectDir)BuildScripts call switch_client_debug.bat $(ProjectDir)ts site.ts true false 

Now everything works like a charm, and I have the ability to log, track, special logic to configure Debug and for Release in Javascript.

+2
source

All Articles