'console' is undefined error for Internet Explorer

前端 未结 21 1301
-上瘾入骨i
-上瘾入骨i 2020-11-22 04:49

I\'m using Firebug and have some statements like:

console.log(\"...\");

in my page. In IE8 (probably earlier versions too) I get script err

相关标签:
21条回答
  • 2020-11-22 05:21

    You can use console.log() if you have Developer Tools in IE8 opened and also you can use the Console textbox on script tab.

    0 讨论(0)
  • 2020-11-22 05:21

    For IE8 or console support limited to console.log (no debug, trace, ...) you can do the following:

    • If console OR console.log undefined: Create dummy functions for console functions (trace, debug, log, ...)

      window.console = { debug : function() {}, ...};

    • Else if console.log is defined (IE8) AND console.debug (any other) is not defined: redirect all logging functions to console.log, this allows to keep those logs !

      window.console = { debug : window.console.log, ...};

    Not sure about the assert support in various IE versions, but any suggestions are welcome. Also posted this answer here: How can I use console logging in Internet Explorer?

    0 讨论(0)
  • 2020-11-22 05:22

    In my scripts, I either use the shorthand:

    window.console && console.log(...) // only log if the function exists
    

    or, if it's not possible or feasible to edit every console.log line, I create a fake console:

    // check to see if console exists. If not, create an empty object for it,
    // then create and empty logging function which does nothing. 
    //
    // REMEMBER: put this before any other console.log calls
    !window.console && (window.console = {} && window.console.log = function () {});
    
    0 讨论(0)
  • 2020-11-22 05:23

    For debugging in IE, check out this log4javascript

    0 讨论(0)
  • 2020-11-22 05:24

    I'm using fauxconsole; I modified the css a bit so that it looks nicer but works very well.

    0 讨论(0)
  • 2020-11-22 05:25

    Based on two previous answers by

    • Vinícius Moraes
    • Peter Tseng

    and the documentations for

    • Internet Explorer (IE 10)
    • Safari (2012. 07. 23.)
    • Firefox (2013. 05. 20.)
    • Chrome (2013. 01. 25.) and Chrome (2012. 10. 04.)
    • and some of my knowledge

    Here's a best effort implementation for the issue, meaning if there's a console.log which actually exists, it fills in the gaps for non-existing methods via console.log.

    For example for IE6/7 you can replace logging with alert (stupid but works) and then include the below monster (I called it console.js): [Feel free to remove comments as you see fit, I left them in for reference, a minimizer can tackle them]:

    <!--[if lte IE 7]>
    <SCRIPT LANGUAGE="javascript">
        (window.console = window.console || {}).log = function() { return window.alert.apply(window, arguments); };
    </SCRIPT>
    <![endif]-->
    <script type="text/javascript" src="console.js"></script>
    

    and console.js:

        /**
         * Protect window.console method calls, e.g. console is not defined on IE
         * unless dev tools are open, and IE doesn't define console.debug
         */
        (function() {
            var console = (window.console = window.console || {});
            var noop = function () {};
            var log = console.log || noop;
            var start = function(name) { return function(param) { log("Start " + name + ": " + param); } };
            var end = function(name) { return function(param) { log("End " + name + ": " + param); } };
    
            var methods = {
                // Internet Explorer (IE 10): http://msdn.microsoft.com/en-us/library/ie/hh772169(v=vs.85).aspx#methods
                // assert(test, message, optionalParams), clear(), count(countTitle), debug(message, optionalParams), dir(value, optionalParams), dirxml(value), error(message, optionalParams), group(groupTitle), groupCollapsed(groupTitle), groupEnd([groupTitle]), info(message, optionalParams), log(message, optionalParams), msIsIndependentlyComposed(oElementNode), profile(reportName), profileEnd(), time(timerName), timeEnd(timerName), trace(), warn(message, optionalParams)
                // "assert", "clear", "count", "debug", "dir", "dirxml", "error", "group", "groupCollapsed", "groupEnd", "info", "log", "msIsIndependentlyComposed", "profile", "profileEnd", "time", "timeEnd", "trace", "warn"
    
                // Safari (2012. 07. 23.): https://developer.apple.com/library/safari/#documentation/AppleApplications/Conceptual/Safari_Developer_Guide/DebuggingYourWebsite/DebuggingYourWebsite.html#//apple_ref/doc/uid/TP40007874-CH8-SW20
                // assert(expression, message-object), count([title]), debug([message-object]), dir(object), dirxml(node), error(message-object), group(message-object), groupEnd(), info(message-object), log(message-object), profile([title]), profileEnd([title]), time(name), markTimeline("string"), trace(), warn(message-object)
                // "assert", "count", "debug", "dir", "dirxml", "error", "group", "groupEnd", "info", "log", "profile", "profileEnd", "time", "markTimeline", "trace", "warn"
    
                // Firefox (2013. 05. 20.): https://developer.mozilla.org/en-US/docs/Web/API/console
                // debug(obj1 [, obj2, ..., objN]), debug(msg [, subst1, ..., substN]), dir(object), error(obj1 [, obj2, ..., objN]), error(msg [, subst1, ..., substN]), group(), groupCollapsed(), groupEnd(), info(obj1 [, obj2, ..., objN]), info(msg [, subst1, ..., substN]), log(obj1 [, obj2, ..., objN]), log(msg [, subst1, ..., substN]), time(timerName), timeEnd(timerName), trace(), warn(obj1 [, obj2, ..., objN]), warn(msg [, subst1, ..., substN])
                // "debug", "dir", "error", "group", "groupCollapsed", "groupEnd", "info", "log", "time", "timeEnd", "trace", "warn"
    
                // Chrome (2013. 01. 25.): https://developers.google.com/chrome-developer-tools/docs/console-api
                // assert(expression, object), clear(), count(label), debug(object [, object, ...]), dir(object), dirxml(object), error(object [, object, ...]), group(object[, object, ...]), groupCollapsed(object[, object, ...]), groupEnd(), info(object [, object, ...]), log(object [, object, ...]), profile([label]), profileEnd(), time(label), timeEnd(label), timeStamp([label]), trace(), warn(object [, object, ...])
                // "assert", "clear", "count", "debug", "dir", "dirxml", "error", "group", "groupCollapsed", "groupEnd", "info", "log", "profile", "profileEnd", "time", "timeEnd", "timeStamp", "trace", "warn"
                // Chrome (2012. 10. 04.): https://developers.google.com/web-toolkit/speedtracer/logging-api
                // markTimeline(String)
                // "markTimeline"
    
                assert: noop, clear: noop, trace: noop, count: noop, timeStamp: noop, msIsIndependentlyComposed: noop,
                debug: log, info: log, log: log, warn: log, error: log,
                dir: log, dirxml: log, markTimeline: log,
                group: start('group'), groupCollapsed: start('groupCollapsed'), groupEnd: end('group'),
                profile: start('profile'), profileEnd: end('profile'),
                time: start('time'), timeEnd: end('time')
            };
    
            for (var method in methods) {
                if ( methods.hasOwnProperty(method) && !(method in console) ) { // define undefined methods as best-effort methods
                    console[method] = methods[method];
                }
            }
        })();
    
    0 讨论(0)
提交回复
热议问题