console.log throws error on Internet Explorer IE
If you have calls to console.log in your Javascript, you will get an error in Internet Explorer. This is because there is no console variable (object) in the window name space.
Sure you could take out all your console.log statements, but usually you need those around for a little while. Here is a quick snippet you can use in JQuery that'll take the place of console.log. Your Firefox console will still work and you won't get errors on Internet Explorer.
2 if( (window['console'] !== undefined) ){
3 console.log( text );
4 }
5}
Now when you want to log something, you can use:
2$.logThis( "I pity the fool who uses Internet Explorer");
Happy Days are here again!
There are no comments for this entry.


If I am debugging in IE I usually load the Firebug Lite javascript. That is a nice trick to keep things working without having to load additional scripts though.
I think Bucky's solution is better because there's less customization taking place...fewer moving parts, cleaner code, etc.
Dan's jQuery plugin would be useful if it was doing something other than just blocking the errors in IE.
@Ty,
I'm sure Bucky appreciates your vote of confidence. You might be right, though there is one point worthy of consideration.
If you use my approach across your site, you've bottlenecked all logging calls to a central place. Should you run into a situation where you need to debug IE (which happens from time to time), you could easily change the logging behaviour to send a quick ajax call to your application and log calls in a database or append them to another window.
With Bucky's method, you don't have a single point of logging, thus you aren't easily going to be able to do the above.
That said, Bucky is a smart guy and he generally has good ideas.
DW
@Dan, good point, and I just ran into what you describe. I need to display the logging in IE. So Bucky's solution is great for using console.log in Firefox and preventing errors in IE, but if logging is required in IE, your solution works better.
It's always good to have lots of tools available. Thanks to both of you!
Bucky's approach can be combined with whatever you like, like this:
if (typeof console === 'undefined') {
console = { log : function(){ alert(arguments[0]);} };
}
Naturally you can loop through arguments to display all arguments passed to console.log, also you don't have to alert it - but this provides the same hook/handle...
You can reduce the number of processes performed by changing your code to the below:
if( (window['console'] !== undefined) )
{
jQuery.log = function( text )
{
console.log( text );
}
}
So instead of checking if the console exists every time you log something, you're only checking it once, which is all that is required.
Russell's code doesn't work for me-- specifically it works OK on browsers where 'console' is defined. But it does not work on IE, you get an error when it hits jQuery.log(). Which is what we are trying to avoid in the first place.
Actualy, you can change Russell's code to handle console == 'undefined'
You could do
if( (window['console'] !== undefined) )
{
jQuery.log = function( text )
{
console.log( text );
}
}
else
{
jQuery.log = function(text)
{
// implement debugging for browsers with no console
}
}
IE is making my scripts ugly.
Nice tip. If you already have console.log spread throughout your code and you don't want to remove all instances of it, you can add this code snippet to the top of your first JavaScript file:
if console === undefined {
console = { log : function(){} };
}