What happened to console.log in IE8?

后端 未结 17 2484
爱一瞬间的悲伤
爱一瞬间的悲伤 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:49

    I really like the approach posted by "orange80". It's elegant because you can set it once and forget it.

    The other approaches require you to do something different (call something other than plain console.log() every time), which is just asking for trouble… I know that I'd eventually forget.

    I've taken it a step further, by wrapping the code in a utility function that you can call once at the beginning of your javascript, anywhere as long as it's before any logging. (I'm installing this in my company's event data router product. It will help simplify the cross-browser design of its new admin interface.)

    /**
     * Call once at beginning to ensure your app can safely call console.log() and
     * console.dir(), even on browsers that don't support it.  You may not get useful
     * logging on those browers, but at least you won't generate errors.
     * 
     * @param  alertFallback - if 'true', all logs become alerts, if necessary. 
     *   (not usually suitable for production)
     */
    function fixConsole(alertFallback)
    {    
        if (typeof console === "undefined")
        {
            console = {}; // define it if it doesn't exist already
        }
        if (typeof console.log === "undefined") 
        {
            if (alertFallback) { console.log = function(msg) { alert(msg); }; } 
            else { console.log = function() {}; }
        }
        if (typeof console.dir === "undefined") 
        {
            if (alertFallback) 
            { 
                // THIS COULD BE IMPROVED… maybe list all the object properties?
                console.dir = function(obj) { alert("DIR: "+obj); }; 
            }
            else { console.dir = function() {}; }
        }
    }
    

提交回复
热议问题