What happened to console.log in IE8?

后端 未结 17 2517
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 04:45

According to this post it was in the beta, but it\'s not in the release?

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

    It works in IE8. Open IE8's Developer Tools by hitting F12.

    >>console.log('test')
    LOG: test
    
    0 讨论(0)
  • 2020-11-22 05:39

    If you get "undefined" to all of your console.log calls, that probably means you still have an old firebuglite loaded (firebug.js). It will override all the valid functions of IE8's console.log even though they do exist. This is what happened to me anyway.

    Check for other code overriding the console object.

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

    This is my take on the various answers. I wanted to actually see the logged messages, even if I did not have the IE console open when they were fired, so I push them into a console.messages array that I create. I also added a function console.dump() to facilitate viewing the whole log. console.clear() will empty the message queue.

    This solutions also "handles" the other Console methods (which I believe all originate from the Firebug Console API)

    Finally, this solution is in the form of an IIFE, so it does not pollute the global scope. The fallback function argument is defined at the bottom of the code.

    I just drop it in my master JS file which is included on every page, and forget about it.

    (function (fallback) {    
    
        fallback = fallback || function () { };
    
        // function to trap most of the console functions from the FireBug Console API. 
        var trap = function () {
            // create an Array from the arguments Object           
            var args = Array.prototype.slice.call(arguments);
            // console.raw captures the raw args, without converting toString
            console.raw.push(args);
            var message = args.join(' ');
            console.messages.push(message);
            fallback(message);
        };
    
        // redefine console
        if (typeof console === 'undefined') {
            console = {
                messages: [],
                raw: [],
                dump: function() { return console.messages.join('\n'); },
                log: trap,
                debug: trap,
                info: trap,
                warn: trap,
                error: trap,
                assert: trap,
                clear: function() { 
                      console.messages.length = 0; 
                      console.raw.length = 0 ;
                },
                dir: trap,
                dirxml: trap,
                trace: trap,
                group: trap,
                groupCollapsed: trap,
                groupEnd: trap,
                time: trap,
                timeEnd: trap,
                timeStamp: trap,
                profile: trap,
                profileEnd: trap,
                count: trap,
                exception: trap,
                table: trap
            };
        }
    
    })(null); // to define a fallback function, replace null with the name of the function (ex: alert)
    

    Some extra info

    The line var args = Array.prototype.slice.call(arguments); creates an Array from the arguments Object. This is required because arguments is not really an Array.

    trap() is a default handler for any of the API functions. I pass the arguments to message so that you get a log of the arguments that were passed to any API call (not just console.log).

    Edit

    I added an extra array console.raw that captures the arguments exactly as passed to trap(). I realized that args.join(' ') was converting objects to the string "[object Object]" which may sometimes be undesirable. Thanks bfontaine for the suggestion.

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

    Even better for fallback is this:

    
       var alertFallback = true;
       if (typeof console === "undefined" || typeof console.log === "undefined") {
         console = {};
         if (alertFallback) {
             console.log = function(msg) {
                  alert(msg);
             };
         } else {
             console.log = function() {};
         }
       }
    
    
    0 讨论(0)
  • 2020-11-22 05:43

    I found this on github:

    // usage: log('inside coolFunc', this, arguments);
    // paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
    window.log = function f() {
        log.history = log.history || [];
        log.history.push(arguments);
        if (this.console) {
            var args = arguments,
                newarr;
            args.callee = args.callee.caller;
            newarr = [].slice.call(args);
            if (typeof console.log === 'object') log.apply.call(console.log, console, newarr);
            else console.log.apply(console, newarr);
        }
    };
    
    // make it safe to use console.log always
    (function(a) {
        function b() {}
        for (var c = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","), d; !! (d = c.pop());) {
            a[d] = a[d] || b;
        }
    })(function() {
        try {
            console.log();
            return window.console;
        } catch(a) {
            return (window.console = {});
        }
    } ());
    
    0 讨论(0)
  • 2020-11-22 05:46

    The best solution for any browser that lack a console is:

    // 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]) {
                console[method] = noop;
            }
        }
    }());
    
    0 讨论(0)
提交回复
热议问题