views:

57

answers:

2

Hi,

I'm using the:

console.log()

method to log messages to firefox (3.6.6)/firebug while working on my webapp. When I view the app locally, it works fine, log messages come up ok. When I push my app up to my live server and view the page, I get lots of 'console not defined' errors.

I am not quite certain how the console object was even resolved in the first place, since I don't have any js includes for it in the first place. What's the right way to use the console object?

Thanks

http://getfirebug.com/logging

----------------- Edit ----------------------------

Yeah I am using the same browser (FF) - I just pushed the project to the live host, and I only get the errors there. What's strange though is that some of the console statements are working now, others just still give the error. Copy-pasting here as a sanity check:

console is not defined [Break on this error] console.log(window.location);

console is not defined [Break on this error] console.log(farmAttrAsJson);

the second statement above is logged when clicking a button. So the first time I clicked, got that error. Waited a few minutes, clicked again, and then it logged ok.

+2  A: 

The console object is not defined in FF unless Firebug is open.

In Chrome it's always defined.

One way to handle it is to define it if it is not defined:

if(!window.console) console = {log: function() {}};
sje397
Ah cool yeah that makes sense, I'll add it, thank you.
A: 

i found this one which looks even better because it has all the console methods. not just log

(function(){
   if (!window.console||!console.firebug){
  var methods = [
     "log", "debug", "info", "warn", "error", "assert",
     "dir", "dirxml", "group", "groupEnd", "time", "timeEnd",
     "count", "trace", "profile", "profileEnd"
  ];
  window.console = {};
  for (var i=0; i<methods.length; i++){
     window.console[methods[i]] = function(){};
  }
  }
  })();
guy schaller