'console' is undefined error for Internet Explorer

前端 未结 21 1401
-上瘾入骨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:15

    Encountered similar problem running console.log in child windows in IE9, created by window.open function.

    It seems that in this case console is defined only in parent window and is undefined in child windows until you refresh them. Same applies to children of child windows.

    I deal with this issue by wrapping log in next function (below is fragment of module)

    getConsole: function()
        {
            if (typeof console !== 'undefined') return console;
    
            var searchDepthMax = 5,
                searchDepth = 0,
                context = window.opener;
    
            while (!!context && searchDepth < searchDepthMax)
            {
                if (typeof context.console !== 'undefined') return context.console;
    
                context = context.opener;
                searchDepth++;
            }
    
            return null;
        },
        log: function(message){
            var _console = this.getConsole();
            if (!!_console) _console.log(message);
        }
    

提交回复
热议问题