Is there a way to log to console without breaking code under IE?

前端 未结 6 556
后悔当初
后悔当初 2021-02-05 18:50

I\'m trying to use console.log to put some logging into the javascript side of my program. I noticed, though, that unless the dev console is open in IE, JS basically stops worki

6条回答
  •  借酒劲吻你
    2021-02-05 19:29

    I use this snippet myself

    if (! ('console' in window) ) {
    var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
    window.console = {};
    for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};
    }else {
    /*if it exists but doesn't contain all the same methods....silly ie*/
    var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
    for (var i = 0; i < names.length; ++i) if(!window.console[names[i]])window.console[names[i]] = function() {};
    };
    

提交回复
热议问题