Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug for javascript code

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
like image 210
TK1 Avatar asked Aug 30 '26 22:08

TK1


1 Answers

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)

like image 189
spender Avatar answered Sep 01 '26 12:09

spender