简体   繁体   中英

firebug console

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM