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.

view plain print about
1jQuery.logThis = function( text ){
2    if( (window['console'] !== undefined) ){
3        console.log( text );
4    }
5}

Now when you want to log something, you can use:

view plain print about
1JQuery.logThis( "I pity the fool who uses console.log() ");
2$.logThis( "I pity the fool who uses Internet Explorer");

Happy Days are here again!

There are no comments for this entry.

Add Comment Subscribe to Comments

7/12/10 10:49 AM # Posted By Bucky Schwarz

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(){} };
}


7/12/10 10:52 AM # Posted By Robert Zehnder

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.


7/22/10 11:01 AM # Posted By Ty

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.


7/22/10 11:34 AM # Posted By Dan Wilson

@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


9/30/10 2:39 PM # Posted By Ty

@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!


4/15/11 4:29 PM # Posted By Chuck Ott

Thanks, Dan! I've been looking for something like this.


8/22/11 7:53 AM # Posted By Per Hornshøj-Schierbeck

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...


2/29/12 8:45 AM # Posted By Russell

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.


5/11/12 11:46 AM # Posted By Tom Graham

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.


6/13/12 7:40 AM # Posted By YinYang

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.


Add Comment Subscribe to Comments