【问题标题】:Common idiom to avoid IE throw: Error: 'console' is undefined避免 IE 抛出的常见习语:错误:'console' is undefined
【发布时间】:2009-07-18 00:38:22
【问题描述】:

我已经安装了 firebug 并编写了所有这些日志语句。

我已经在 IE 中测试了我的应用程序,当然我遇到了“未定义”错误。

避免这种情况的常用习语是什么。

我真的不想评论我文件中的所有 console.log 语句,也不想模拟它们。

嗯,我不知道该怎么办。

【问题讨论】:

    标签: javascript console firebug undefined


    【解决方案1】:

    我通常会像这样制作一个包装函数:

    function log(obj) {
        if (window.console && console.log) console.log(obj);
    }
    

    或者你可以在你的脚本文件/元素的开头做这样的事情:

    if (!window.console) { 
        window.console = {
            log: function(obj){ /* define own logging function here, or leave empty */ }
        };
    }
    

    【讨论】:

    • 并在调试时注释/取消注释该单行?
    • 啊,不,它会在 firefox+firebug 上返回 false :)
    【解决方案2】:

    Paul Irish 为 console.log() 提供了更好的包装器。

    http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

    这允许多个参数,并提供历史记录(用于调试)以防没有控制台或(例如 Firebug Lite)稍后创建控制台。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多