Search

Using window.console in JScript

just to log messages in browser console, the console must be visible, the below function is a foolproof way to implement it, regardless of the browser.
 
/* Jscript */
//console
(function() {
if (!window.console) {
window.console = {};
}
// union of Chrome, FF, IE, and Safari console methods
var m = [
"log", "info", "warn", "error", "debug", "trace", "dir", "group",
"groupCollapsed", "groupEnd", "time", "timeEnd", "profile", "profileEnd",
"dirxml", "assert", "count", "markTimeline", "timeStamp", "clear"
];
// define undefined methods as noops to prevent errors
for (var i = 0; i < m.length; i++) {
if (!window.console[m[i]]) {
window.console[m[i]] = function() {};
}
}
})();


now in you can write anywhere
 window.console.warn(" this is warning");

to print in the console, this is quite useful with the old IE browsers.