A small yet useful script from Henrik Joreteg to conditionally turn on client-side debugging in web apps. Instead of having to wrap console.log to include a conditional, Henrik looks for a debug flag in localStorage:

// follow @HenrikJoreteg and @andyet if you like this ;)
(function () {
if (localStorage && localStorage.debug && window.console) {
    if (typeof exports !== 'undefined') {
        module.exports = window.console;
    } else {
        // overwrite it... yeah, weird, but it works.
        window.console = window.console;
    }
} else {
    var methods = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),
        l = methods.length,
        i = 0,
        res = {},
        fn = function () {};

    for (; i < l; i++) {
        res[methods[i]] = fn;
    }

    if (typeof exports !== 'undefined') {
        module.exports = res;
    } else {
        window.console = res;
    }
}
})();

Check out the README for usage and CommonJS support details.


Have comments? Send a tweet to @TheChangelog on Twitter.

Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.