I\'m using Firebug and have some statements like:
console.log(\"...\");
in my page. In IE8 (probably earlier versions too) I get script err
You can use the below to give an extra degree of insurance that you've got all bases covered. Using typeof
first will avoid any undefined
errors. Using ===
will also ensure that the name of the type is actually the string "undefined". Finally, you'll want to add a parameter to the function signature (I chose logMsg
arbitrarily) to ensure consistency, since you do pass whatever you want printed to the console to the log function. This also keep you intellisense accurate and avoids any warnings/errors in your JS aware IDE.
if(!window.console || typeof console === "undefined") {
var console = { log: function (logMsg) { } };
}
Stub of console in TypeScript:
if (!window.console) {
console = {
assert: () => { },
clear: () => { },
count: () => { },
debug: () => { },
dir: () => { },
dirxml: () => { },
error: () => { },
group: () => { },
groupCollapsed: () => { },
groupEnd: () => { },
info: () => { },
log: () => { },
msIsIndependentlyComposed: (e: Element) => false,
profile: () => { },
profileEnd: () => { },
select: () => { },
time: () => { },
timeEnd: () => { },
trace: () => { },
warn: () => { },
}
};
Encountered similar problem running console.log in child windows in IE9, created by window.open function.
It seems that in this case console is defined only in parent window and is undefined in child windows until you refresh them. Same applies to children of child windows.
I deal with this issue by wrapping log in next function (below is fragment of module)
getConsole: function()
{
if (typeof console !== 'undefined') return console;
var searchDepthMax = 5,
searchDepth = 0,
context = window.opener;
while (!!context && searchDepth < searchDepthMax)
{
if (typeof context.console !== 'undefined') return context.console;
context = context.opener;
searchDepth++;
}
return null;
},
log: function(message){
var _console = this.getConsole();
if (!!_console) _console.log(message);
}
After having oh so many problems with this thing (it's hard to debug the error since if you open the developer console the error no longer happens!) I decided to make an overkill code to never have to bother with this ever again:
if (typeof window.console === "undefined")
window.console = {};
if (typeof window.console.debug === "undefined")
window.console.debug= function() {};
if (typeof window.console.log === "undefined")
window.console.log= function() {};
if (typeof window.console.error === "undefined")
window.console.error= function() {alert("error");};
if (typeof window.console.time === "undefined")
window.console.time= function() {};
if (typeof window.console.trace === "undefined")
window.console.trace= function() {};
if (typeof window.console.info === "undefined")
window.console.info= function() {};
if (typeof window.console.timeEnd === "undefined")
window.console.timeEnd= function() {};
if (typeof window.console.group === "undefined")
window.console.group= function() {};
if (typeof window.console.groupEnd === "undefined")
window.console.groupEnd= function() {};
if (typeof window.console.groupCollapsed === "undefined")
window.console.groupCollapsed= function() {};
if (typeof window.console.dir === "undefined")
window.console.dir= function() {};
if (typeof window.console.warn === "undefined")
window.console.warn= function() {};
Personaly I only ever use console.log and console.error, but this code handles all the other functions as shown in the Mozzila Developer Network: https://developer.mozilla.org/en-US/docs/Web/API/console. Just put that code on the top of your page and you are done forever with this.
Noticed that OP is using Firebug with IE, so assume it's Firebug Lite. This is a funky situation as console gets defined in IE when the debugger window is opened, but what happens when Firebug is already running? Not sure, but perhaps the "firebugx.js" method might be a good way to test in this situation:
source:
https://code.google.com/p/fbug/source/browse/branches/firebug1.2/lite/firebugx.js?r=187
if (!window.console || !console.firebug) {
var names = [
"log", "debug", "info", "warn", "error", "assert",
"dir","dirxml","group","groupEnd","time","timeEnd",
"count","trace","profile","profileEnd"
];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {}
}
(updated links 12/2014)
console = console || {
debug: function(){},
log: function(){}
...
}