Firebug console

The following is my block of code. since I did not install Firebug in IE, every time I try to check my code in IE , I get a console undefined error message. so I decided and developed this code block, so console.log only works in firefox and avoids error messages in IE .

 function clog() { if(window.console && window.console.firebug) { var a =[]; for(var i=0;i<arguments.length;i++) { a.push(arguments[i]); } console.log(a.join(' , ')); } } 

my code is working fine and I get the results that I wanted

but when I tried to use the above code in jQuery (e.g. clog($('body')); ),

the result i was expecting should be jQuery(body) . but I get the result as [object Object]

How can I get the results that I expected?

Thanks!

+4
source share
3 answers
 console.log(a); 

instead

 console.log(a.join(' , ')); 

must do it.

Array.prototype.join will concatenate all array entries into String . It means

 var b = [{}, "test"]; b.toString() 

will be evaluated to "[object Object],test" , regardless of whether methods or members are inside this object. You simply lose this information by calling .toString() .

+2
source

When you call a selector like this, tell $('body') what you are doing is to create an object, a jQuery object ... so your output is correct.

If you want to display something other than .toString() , you must call this property, for example:

 $('body').selector //body $('body').length //1 $('body').context //document 

If all you use is console.log , I find that I just create it if it is absent (as opposed to checking whenever you want to use it) much easier, just do this run before any registration code:

 if (typeof console == "undefined") console = { log: function () { } }; 

Then you can delete the current if check.

+4
source

I always write a wrapper function (so that non-working "console" browsers have a problem)

 function log(msg) { try { console.log(msg); } catch(e){} } 

You can check the msg object, then check the type to determine if it is a jQuery object and retrieve the data.

+3
source

All Articles