Fixing JavaScript Console in older browsers and IE

Thursday, May 17. 2012

Fixing JavaScript Console in older browsers and IE

The JavaScript console as introduced by Firebug years ago has grown to a de facto standard in many web browsers. Especially the Webkit JavaScript console in Chrome/Chromium and Safari is extremely well done. Newer Firefox versions now also come with a built-in console. Even Internet Explorer comes with a basic console-like thing but unfortunately it sucks...

So far I used custom logging mechanisms or log4javascript to log messages in my JavaScript applications. Thanks to the JavaScript console this would be unnecessary if all browsers had descent support for it. But unfortunately the console implementations in some browsers are incomplete or completely absent. So even this harmless line of code in your application can prevent execution in Internet Explorer when the development panel is closed (In which case window.console is not defined at all):

console.warn("Config not found. Using defaults.");

So to be able to use all console features without worrying about missing browser-support I wrote a small library called console-shim which detects the implemented console features and tries to complete them. Some features are emulated so they fully work in all browsers while other features are silently ignored. If console logging is not supported by the browser at all then console-shim tries to use log4javascript if available or it silently ignores all logging attempts. See the README for a full list of replacement implementations for missing functionality.

Posted in JavaScript | Comments (0)

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.