Internet Explorer: “console is not defined” Error

前端 未结 9 1614
天涯浪人
天涯浪人 2020-12-05 18:33

I was using console.log() in some JavaScript I wrote and an error of: console is not defined was thrown in Internet Explorer (worked fine in other

9条回答
  •  悲&欢浪女
    2020-12-05 19:05

    Edit of @yckart's answer

    Using c.length as input to a function which defines c won't work. Also you're just reassigning items in the array with noop when you should be adding methods to window.console.

    (function(w){
      var c = 'assert,clear,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,table,time,timeEnd,timeStamp,trace,warn'.split(','),
      noop = function () {};
    
      w.console = w.console || (function (len) {
        var ret = {};
        while (len--) { ret[c[len]] = noop; }
        return ret;
      }(c.length));
    })(window);
    

提交回复
热议问题