|
Working with firebug console is very easy and increase productivity, because Firebug console provide you with easy to use logging methods. But when you close Firebug you can get error "console is not defined". It's because console object exists only when Firebug is open.
The solution of this problem can be very easy:
Firebug console.log for Internet Explorer
Internet explorer 8 also supports console object and all the methods from Firebug like console.log, console.error, console.info.... But unlike Firefox console object exists and defined in Internet explorer all the time. It does not depend on Developer toolbar open or closed.
How to check if console object exists
I believe the best option avoid error "console is not defined" is use some wrapper class. But for quick fixes you can use code to check if console object exists in browser. I know at least two solutions how to check if console defined:
-
The first is to check if console object belongs to window object
-
And the second is to check if console is 'undefined'
For the first approach I would recommend this code
if('console' in window && 'log' in window.console)
{
// code using console.log here
}
For the second approach - this code:
if(typeof window.console != 'undefined'
&& typeof window.console.log != 'undefined')
{
// code using console.log here
}
Both works fine in all types of browsers.
|