views:

80

answers:

3
+5  Q: 

firebug console

Hello,

The following is my block of code. since I haven't installed Firebug in IE, Every time when I try to test my code in IE I'm getting an error message console is undefined. so I decided and developed this block of code, so that console.log work only in firefox and to avoid 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'm getting the results which I wanted,

but when I tried to use the above code on jQuery ( for example clog($('body')); ),

the result which I expected is to be jQuery(body) . but I'm getting the result as [object Object]

How can I get The results which I expected ?

Thank you !!

+3  A: 

When you call a selector like that, say $('body') what you're doing is creating an object, a jQuery object...so your output is correct.

If you want to display something other than it's .toString(), then you should call that property, for example:

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

If all you're using is console.log, I find just creating it if it's missing (as opposed to checking whenever you want to use it) is much easier, just have this run before any of your logging code:

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

Then you can remove your current if check.

Nick Craver
To be more correct: it's called a JavaScript object, not a jQuery object. ;)
Marcel Korpel
@Marcel - it's both...it's literally an instance of a jQuery object, like an Array is a JavaScript object, but *specifically* it's an Array :)
Nick Craver
Why doesn't `if(window.console)` work correctly? It will evaluate to `false` if `console` is not available.
Marcel Korpel
@Marcel - Not in IE...had this issue yesterday, don't ask me why, even in IE8 this still breaks, haven't tested in 9 though.
Nick Craver
Ah, jQuery extends the default object? I'm not a jQuery expert, I merely thought it returned the `getElementById` object.
Marcel Korpel
So `if(window.XMLHttpRequest)` will also cause an error in IE? I've seen this kind of code in many places (and used it myself, too).
Marcel Korpel
@Marcel - It's internally an Array, that represents the DOM elements the selector found (or were added, in the chain etc), so it doesn't contain the objects, but an array of references to the DOM elements, any action you perform gets applied to all of them, e.g. `$(".myClass").fadeOut()` would fade out *all* `class="myClass"` elements the selector found.
Nick Craver
@Nick: did you try (just to be sure) to use `if('console' in window)` instead of `if(window.console)` in IE ?
jAndy
@jAndy - I'm double checking now, it seems compatibility view screws with this too, I'm not sure why here.
Nick Craver
@jAndy - Compatibility mode breaks `if(!window.console)` here but not in a simple test...time to write a fun email to a plugin author screwing with my console definition.
Nick Craver
@Nick: Greetings to him
jAndy
+3  A: 
console.log(a);

instead of

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

should do it.

Array.prototype.join will concatenate all array entrys into a String. That means

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

will evaluate to "[object Object],test" regardless what methods or members are within that object. You just lose that information calling .toString().

jAndy
+4  A: 

I always write a wrapper function (to keep non 'console' browers from having problem)

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

You could examine the "msg" object, then check the type to determine whether it's a "jQuery" object, and extract the data.

Derrick