I am working on a MVC application, and would like some code in JavaScript to run only if I am in debug mode. I do not want that code to run when I release the code.
In other words, is there anything similar to the following code in C#, for javascript / jQuery?
#if (DEBUG)
// debugging code block here
#else
// release code block here
#endif
I'd suggest using the construct in your question and setting something in your viewmodel to include/exclude the javascript.
public ActionResult XXX()
{
var vm=new MyViewModel(); //or just use ViewBag/ViewData
#if (DEBUG)
vm.RunJS=true;
#else
vm.RunJS=false;
#endif
return View(vm);
}
then in your view
@if(Model.RunJS)
{
<script ...></script>
}
or use a similar construct to pass the DEBUG status through to your javascript.
<script type="text/javascript">
startMyJavascript(@Model.IsDebug?"true":"false");
</script>
(not so sure about Razor syntax, but I think the above is good)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With