2

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
3
  • following code in C# for javascript some code in javascript Which language?! Above snippet looks fine... Commented Jul 23, 2013 at 22:45
  • Just create a global called DEBUG, but you should really remove all debugging code on a production site, so I don't see the point ? Commented Jul 23, 2013 at 22:45
  • the way my application is designed, I use a different set of variables when debugging than in production. so I do need this kind of a functionality for my application.. Commented Jul 23, 2013 at 22:49

3 Answers 3

5

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)

Sign up to request clarification or add additional context in comments.

4 Comments

+1, debug code in JavaScript is submitted regardless if it's required/not. If you can stop the code directly from the back-end that would be the best option.
I want to run parts of the code in the JS file, depending upon the debug state. I don't want to include / exclude files ...
@TK1: Javascript running in the browser has no awareness of the DEBUG (or otherwise) status of your backend. You need to pass it through the View as I suggest above.
@TK1 this isn't including files.. in ASP.NET when you do this: if(1 == 2)alert("hey") else alert("hi") the javascript received by the browser is: alert("hi")
4

Sure. Here is the JavaScript version.

var DEBUG = true;

if(DEBUG) {
    // debug code
} else {
    // production code
}

2 Comments

oh, I wanted to know if I can use the variable from the visual studio compiler. Basically, I can compile code from visual studio, under Debug mode, or release mode ...
I ended up using this kind of a solution for this scenario, so I am going to mark this as answer.
2

You can always use the ViewBag along with @Html.Raw(). I do this occasionally and it works well.

Controller:

#if DEBUG
    ViewBag.JavaScript = "String representation of JS code";
#endif
    return View();

View:

@Html.Raw(ViewBag.JavaScript)

I would also recommend using an include such as

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>

rather than the raw JavaScript to avoid having to put all of that mess in your controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.