console.log.apply not working in IE9

后端 未结 7 601
情深已故
情深已故 2020-11-29 00:32

Looks like I\'ve re-invented the wheel, but somehow this isn\'t working in Internet Explorer 9, but does in IE6.

function debug()
  if(!window.console) { 
           


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 00:52

    Try:

    function log(type) {
      if (typeof console !== 'undefined' && typeof console.log !== 'undefined' &&
        console[type] && Function.prototype.bind) {
        var log = Function.prototype.bind.call(console[type], console);
        log.apply(console, Array.prototype.slice.call(arguments, 1));
      }
    }
    log('info', 'test', 'pass');
    log('error', 'test', 'fail');
    

    Works for log, debug, info, warn, error, group or groupEnd.

提交回复
热议问题