Ignore firebug console if it is not installed

I am using Firebug console.log () to debug my site. If I try to view my site in browsers without Firebug, then I get the console is undefined . Is there a way to gracefully avoid this error?

I found this potential solution , but it seems a little cumbersome. And ideas?

+6
javascript firefox internet-explorer console firebug
source share
9 answers

The source code for Firebug provides a file for this:

See firebugx.js

Do not reinvent the wheel every day :)

+17
source share

I always create my cross-browser wrappers for console.log functions and look like this:

function log(a){ try{ console.log(a); }catch(er){ try{ window.opera.postError(a); }catch(er){ //no console avaliable. put //alert(a) here or write to a document node or just ignore } } } 

It can be extended to any browsers. in IE, when in debugging, I would recommend putting this jquery code in the last catch:

 $('body').append('<pre>'+JSON.serialize(a)+'</pre>'); 

You must add JSON.serialize to your script. IE doesn't have it (maybe IE8, I'm not sure)

+4
source share

A related solution is basically an option (with several additional features):

EDIT The code below does not work if firefox is present. This will teach to place code without checking just to show my not-so-operator 1337 || skillz:

 window.console = window.console || {}; console.log = function(){}; 

The reason for this is that the firefox console is actually the recipient of only the off window property . Therefore, we cannot install it. Instead, use something like this:

 if (!window.console) { window.console = {}; window.console.log = function(){}; } 

Also, console.log (and console.warn , console.error ) will work in Webkit browsers, including mobile Safari, pretty cool, huh?

+3
source share

I don’t think this is getting much better than the workaround you are accessing. Of course, you can melt it to a simple definition of console.log() and leave it unchanged, but in essence, you will not bypass a construction like this.

Only the alternative that comes to mind checks console.log every time you call it, and this is even more cumbersome.

+2
source share

Honestly, I would use this. It covers not only console.log() , but any other console method, and in a rather short number of lines. The fact that it was first used in a Yahoo media player seems to suggest that it works fine in a cross browser as well.

This bit of code is your best bet, actually decently elegant and should work in most cases. As long as you comment on the snippet above, why it is needed (so as not to confuse future developers), you should be fine.

+2
source share

The @OcuS solution is more reliable, but you can improve it with mine: Check this way to log in to the FF console: Log into the Firefox error console from JavaScript

Then add these 3 lines inside the IF in firebugx.js:

 window.console['error'] = li window.console['warn'] = li window.console['debug'] = li 

So, you will see the log of each console error , warn and debug , even when Firebug is closed

+1
source share

You can use this code to check if a console object exists

 if('console' in window && 'log' in window.console) { // code using console.log here } 

Or this code

 if(typeof window.console != 'undefined' && typeof window.console.log != 'undefined') { // code using console.log here } 

You can also read this post http://alexandershapovalov.com/firebug-console-is-not-defined-60/ Inside the link for communication, how to create a jQuery shell for the console

+1
source share

My final decision:

 if(!("console" in window)) window.console = {log: function() {}}; 
0
source share

Above my head:

 if(!console) { console = {}; console.log = function() { }; } 
-one
source share

All Articles