Discussion:
Using console.log for debugging causing errors on site
Laurakeet
2009-07-29 20:56:41 UTC
Permalink
If I am using console.log for debugging locally do I need to make sure
I remove those statements once I have pushed my code to a live web
page? What if random users don't have firebug? Will it break my forms
or other pages?

thanks,
Laura
YaoXing Zhang
2009-07-29 21:38:16 UTC
Permalink
Yes it will. And IE users don't have console object at all.
As long as JavsScript doesn't have conditional compilation like other
languages, I prefer defining a flag
var DEBUG = true;
if (DEBUG) console.log(...);

Regards,
YX
Post by Laurakeet
If I am using console.log for debugging locally do I need to make sure
I remove those statements once I have pushed my code to a live web
page? What if random users don't have firebug? Will it break my forms
or other pages?
thanks,
Laura
Mike Ratcliffe
2009-07-30 07:30:18 UTC
Permalink
Actually IE8 Developer Tools (F12) does display console.log messages
in it's console ... console.dir etc. does not work though.
Post by YaoXing Zhang
Yes it will. And IE users don't have console object at all.
As long as JavsScript doesn't have conditional compilation like other
languages, I prefer defining a flag
var DEBUG = true;
if (DEBUG) console.log(...);
Regards,
YX
Post by Laurakeet
If I am using console.log for debugging locally do I need to make sure
I remove those statements once I have pushed my code to a live web
page? What if random users don't have firebug? Will it break my forms
or other pages?
thanks,
Laura
Nicolas Hatier
2009-07-30 18:13:46 UTC
Permalink
I wrote a simple console stub to make it easier to develop on multiple
browsers, and enable/disable console quickly.

The attached script will create a JSConsole object which bears all the
functions supported by Firebug. If you use the JSConsole object in your
code as it was the Firebug console, your code should continue working on
all browsers, whether or not they have consoles. The JSConsole will try
to use the native console function if available, or will use console.log
if the called function isn't there.

By simply switching EnableConsole to false, the JSConsole object will
become inert, still supporting all functions, but not producing any result.

Code can be used, modified, redistributed under a BSD-style license, as
Firebug.

It could be seen like a Firebug-lite-lite :-)

NH
Post by Mike Ratcliffe
Actually IE8 Developer Tools (F12) does display console.log messages
in it's console ... console.dir etc. does not work though.
Post by YaoXing Zhang
Yes it will. And IE users don't have console object at all.
As long as JavsScript doesn't have conditional compilation like other
languages, I prefer defining a flag
var DEBUG = true;
if (DEBUG) console.log(...);
Regards,
YX
Post by Laurakeet
If I am using console.log for debugging locally do I need to make sure
I remove those statements once I have pushed my code to a live web
page? What if random users don't have firebug? Will it break my forms
or other pages?
thanks,
Laura
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Firebug" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to firebug+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/firebug?hl=en
-~----------~----~----~----~------~----~------~--~---

Kara Rawson
2009-07-29 22:44:11 UTC
Permalink
Post by Laurakeet
If I am using console.log for debugging locally do I need to make sure
I remove those statements once I have pushed my code to a live web
page? What if random users don't have firebug? Will it break my forms
or other pages?
thanks,
Laura
yeah, its prolly a good idea to remove those. there are some spiffy txt
editors which can find and remove nodes of htm and such, i think
crimison editor has a feature like this, its nice cuz it searches for
text within multiple files using wildcards and like. leaving the code in
there wont break anything, but i have seen firefox and IE throw some
random errors on console logging stuff.

for more complex apps, you can make a console.log wrapper function which
calls this, then inside this wrapper function put a conditional to check
if debug mode is on or off. So when you go to deploy onto production you
can just toggle that boolean off, and it shouldn't spit that code out to
your end users. Generally i just make it a habbit to remove all of it,
as its not needed and wastes cpu cycles, and bytes.

k
Nicolas Hatier
2009-07-29 22:57:34 UTC
Permalink
I often put something like this at the top of my scripts:

var debug = true;
window.MyConsole = (!debug || !window.console || !console.info) &&
{log:function(){}, debug:function(){}, info:function(){},
warn:function(){}, error:function(){}} || console;

Then I use MyConsole in my scripts. When I go live I just put
Debug=false, and all console code is disabled.

Of course it's better to remove the debug calls, but it's not always
possible.

NH
Post by Laurakeet
If I am using console.log for debugging locally do I need to make sure
I remove those statements once I have pushed my code to a live web
page? What if random users don't have firebug? Will it break my forms
or other pages?
thanks,
Laura
Stefan Weiss
2009-07-29 22:39:43 UTC
Permalink
Post by Laurakeet
If I am using console.log for debugging locally do I need to make sure
I remove those statements once I have pushed my code to a live web
page? What if random users don't have firebug? Will it break my forms
or other pages?
Yes, it will. You could use a DEBUG flag, like YaoXing suggested, or you
could add something like this to your scripts:

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

This will allow you to keep the console.log statements in your code, if
you want, and browsers without a console object won't throw an error.


Stefan
--
LOAD"Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn!",8,1
RUN!
Loading...