Back up

Code Pharaoh

Using web.config debug flag to control JavaScript console log output

#Umbraco #JavaScript

I had an idea (I'm sure i am not the first) to use the config debug status of your Umbraco site to control the JavaScript console logs that exist in your site JavaScript files. To do this I set a global variable in my layout view like this:

var DEBUG = @HttpContext.Current.IsDebuggingEnabled.ToString().ToLower();


Then in one of your script files that is loaded on every page you can add:

if (!DEBUG) {
    if (!window.console) window.console = {};
    var methods = ["log", "debug", "warn", "info"];
    for (var i = 0; i < methods.length; i++) {
        console[methods[i]] = function () { };

So if debug is set to false the console.log/debug warn and info methods will not output anything. 

What are your thoughts, any down sides?