views:

435

answers:

4

I wrote a quick and dirty logger as a jQuery plugin

(function($){   
    $.log = function(debug) {  
     if (console.debug) {   
      console.debug(debug);   
     };  
    };
})(jQuery);

It works fine in Firefox, but in IE7, I'm getting the error

console.debug is null or not an object

How do I perform a function exists in JavaScript that's compatible with IE7?

Thanks

+4  A: 

console.debug is specific to Firebug, which runs under Firefox.

You need to check if window.console is available before checking for console.log!

Here's your code reworked with no errors:

(function($){
    $.log = function(debug) {
     if (window.console && console.debug) {
       console.debug(debug);
     };
    };
})(jQuery);
Jason Berry
ie8 dev tools also has a console
redsquare
So does Safari with developer mode turned on.
alex
Fair points, I don't develop in either so haven't really used their console tools very much.
Jason Berry
I've gone with your solution. Thanks Jason!
alex
No worries at all!
Jason Berry
I would also fallback to creating a hidden div in the footer for browsers without a console. Then at least you can see the log info.
redsquare
+3  A: 

Check if console is defined, then check if debug is a function:

if (typeof(console) != 'undefined' && typeof(console.debug) == 'function'){
  //...
}
CMS
meder
It did throw an error actually `console is undefined`
alex
his modified one should work. though id prefer mine ( if it works ) since its a one liner.
meder
+2  A: 
$.log = window.console && console.debug ? function(debug) { console.debug(debug); } : function(){};

Variations:

$.log = function( debug ) {
    if ( window.console && console.debug ) { 
        console.debug( debug )
    }
}

$.log = function( msg ) {
    if ( window.console ) {
       if ( console.debug ) console.debug ( msg ) 
       else if ( console.log ) console.log ( msg )
    }
}

$.log = function( msg ) {
    if ( typeof console === 'object' && typeof console.debug === 'function' ) { 
       console.debug( msg )
    }
}

$.log = 'console' in window && console.debug ? function(m){console.debug(m)}:function(){}

$.log = function() {
     if ( 'console' in window ) {
         console.debug ? function(m){console.debug(m)} : ( console.log ? function(m){console.log(m)} : function(){}
     }
}

$.log = window.log = function(m){ if(window.console && console.debug) console.debug(m) }
meder
I haven't tried this, but I would assume it would work.
meder
Whoa, went all out on that response, huh? :)
Jason Berry
+1 That's a good response.
alex
A: 

The above answers are all correct, but you're going to have the side effect of your log statements being converted from an arguments object into an array, and your output will look (something) like this:

["my", "debug", "statement"]

To fix that you need to forward the arguments object and keep it intact:

$.log = function() { // note no arguments used
    if ( window.console && console.debug ) { 
        console.debug.apply(console, arguments )
    }
}

Now the output will look like:

My debug statement
mwilcox