• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
山高我为峰
博客园    首页    新随笔    联系   管理    订阅  订阅
防止IE不支持console.log报错

方案一:

var console = console || {
        log : function() {
            return;
        }
    };//兼容IE,当IE不支持console.log时,自定义一个包含log方法的对象给他

方案二:HTML5 Boilerplate 也提供了一个处理所有的 Console call 的 fallback

// Avoid `console` errors in browsers that lack a console.
(function() {
    var method;
    var noop = function () {};
    var methods = [
        'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
        'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
        'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
        'timeStamp', 'trace', 'warn'
    ];
    var length = methods.length;
    var console = (window.console = window.console || {});
    while (length--) {
        method = methods[length];
        // Only stub undefined methods.
        if (!console[method]) {   //对于不支持的方法用noop函数代替
            console[method] = noop;
        }
    }
}());
// Place any jQuery/helper plugins in here.

方案三:自定义封装个方法调用

    function log(msg){
        if (window["console"]){
            console.log(msg);
        }
    }

 

posted on 2015-12-08 11:04  山高我为峰  阅读(961)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3