Last Updated: February 25, 2016
·
1.047K
· 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) {}
    }
}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside