Last Updated: February 25, 2016
·
855
· jcode

JavaScript console debug

Quick JavaScript snippet for logging to the console if available.
If it's not available (mobile browsers, IE, FF without firebug) it won't break the rest of the code.

Also comes with a global debug on/off

var debug = true;


function log(str) {    
    if (debug) {
            var currentTime, h, m, s;
            currentTime = new Date;
            h = currentTime.getHours();
            m = currentTime.getMinutes();
            s = currentTime.getSeconds();
        try {
            console.log("[" + h + ":" + m + ":" + s + "]\t" + str);
        } catch (e) {}
    }
}