Skip to content

Instantly share code, notes, and snippets.

@bjrn
Created October 18, 2012 13:49
Show Gist options
  • Save bjrn/3911945 to your computer and use it in GitHub Desktop.
Save bjrn/3911945 to your computer and use it in GitHub Desktop.
window.console shim
myConsole = {};
(function fixConsole(fakeConsole) {
fakeConsole.debug = true;
fakeConsole.breakOnErrors = true;
var methods = ['log', 'warn', 'error', 'info', 'dir'];
if (!window.console) return;
function intercept(method) {
var realConsole = window.console[method];
fakeConsole[method] = function () {
if (!fakeConsole.debug) return;
// call the real window.console
if (realConsole.apply) {
// Do this for "normal" browsers
realConsole.apply(window.console, arguments);
} else {
// Do this for IE
var message = Array.prototype.slice.apply(arguments).join(' ');
realConsole(message);
}
if (method === "error" && fakeConsole.breakOnErrors) {
if (confirm("Error caught. Do you want to stop javaScript execution?")) {
throw ('execution stopped');
}
}
}
}
for (var i = 0, len = methods.length; i < len; i++)
intercept(methods[i]);
})(myConsole)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment