使用带有firebug的console.log()可以在本地使用,但在发布到我的实时站点时则不行

use*_*701 3 javascript firebug

我正在使用:

console.log()
Run Code Online (Sandbox Code Playgroud)

在我的webapp上工作时将消息记录到firefox(3.6.6)/ firebug的方法.当我在本地查看应用程序时,它工作正常,日志消息就可以了.当我将我的应用程序推送到我的实时服务器并查看页面时,我收到了很多"控制台未定义"错误.

我不太确定控制台对象是如何在第一时间解决的,因为我首先没有任何js包含它.使用控制台对象的正确方法是什么?

谢谢

http://getfirebug.com/logging

-----------------编辑----------------------------

是的我使用相同的浏览器(FF) - 我只是将项目推送到现场主机,我只在那里得到错误.但有些奇怪的是,现在有些控制台语句正在运行,其他语句仍然会出错.复制粘贴在这里作为一个完整性检查:

控制台未定义[中断此错误] console.log(window.location);

控制台未定义[中断此错误] console.log(farmAttrAsJson);

单击按钮时会记录上面的第二个语句.所以我第一次点击,得到了那个错误.等了几分钟,再次点击,然后记录好了.

sje*_*397 6

除非Firebug已打开,否则未在FF中定义控制台对象.

在Chrome中,它始终是定义的.

处理它的一种方法是定义它,如果它没有定义:

if(!window.console) console = {log: function() {}};
Run Code Online (Sandbox Code Playgroud)


guy*_*ler 5

我发现这个看起来更好,因为它有所有的控制台方法.不只是记录

(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(){};
  }
  }
  })();
Run Code Online (Sandbox Code Playgroud)