Skip to content

Instantly share code, notes, and snippets.

@johndkane
Last active December 14, 2015 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johndkane/5158296 to your computer and use it in GitHub Desktop.
Save johndkane/5158296 to your computer and use it in GitHub Desktop.
JavaScript console Polyfill / special case pattern. Basically prevents cross-browser console hell by implementing missing console and empty methods so no JS errors will occur. I found this useful and decided to post a gist. There are many versions of it in the wild; this is only one.
// Run this script before any others.
// Polyfill: provides a missing console and empty implementations for its missing methods.
(function(disable) {
if (!window.console) window.console = { };
var methods = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
for (var i = 0; i < methods.length; i++) {
if (disable || !console[methods[i]]) {
console[methods[i]] = function () { };
}
}
console.log('console not an issue'); // << remove this line after verification of no error
})(false); //'disable' param: false to fill in only missing methods, true to replace *all* console methods with empty implementations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment