Internet Explorer: “console is not defined” Error

前端 未结 9 1600
天涯浪人
天涯浪人 2020-12-05 18:33

I was using console.log() in some JavaScript I wrote and an error of: console is not defined was thrown in Internet Explorer (worked fine in other

相关标签:
9条回答
  • 2020-12-05 18:49

    How about this? Haven't tried it though

    if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} };
    
    0 讨论(0)
  • 2020-12-05 18:53

    If console itself doesn't exist at all, it throws an error because you're accessing an undefined variable. Just like if(abc) {} throws an error.

    Since console resides in window, and window does always exist, this should work:

    if(window.console) ...
    

    Basically, accessing an property that doesn't exist is free and doesn't throw an error (it just evaluates to undefined, failing the if condition). However, it is illegal to access an undeclared variable.

    0 讨论(0)
  • 2020-12-05 18:53

    Other answers gave you the root cause. However, there's a better solution than using if before any call to console.*

    Add this (once) before including any of your scripts that use console:

    //Ensures there will be no 'console is undefined' errors
    window.console = window.console || (function(){
        var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile = c.clear = c.exception = c.trace = c.assert = function(s){};
        return c;
    })();
    

    This will create a 'pseudo' console only if it doesn't exist, so that 'console is undefined' errors will go away and you won't have to ask if console exists everytime. With this, you just call console.log or any console method anywhere, without problems.

    Hope this helps. Cheers

    0 讨论(0)
  • 2020-12-05 18:58

    in internet explorer the console object is not actually defined unless your developer tools are open at the time the window loads.

    to fix your problem, wrap all your console prints in an if statement:

    if (typeof window.console !== 'undefined') {
        ...
    }
    

    you also need to refresh each page after you open the developer tools in order to see the console prints. <3 IE

    0 讨论(0)
  • 2020-12-05 19:00

    You can use the below to give an extra degree of insurance that you've got all bases covered. Using typeof first will avoid any undefined errors. Using === will also ensure that the name of the type is actually the string "undefined". Finally, you'll want to add a parameter to the function signature (I chose logMsg arbitrarily) to ensure consistency, since you do pass whatever you want printed to the console to the log function. This also keep you intellisense accurate and avoids any warnings/errors in your JS aware IDE.

    if(!window.console || typeof console === "undefined") {
      var console = { log: function (logMsg) { } };
    }
    
    0 讨论(0)
  • 2020-12-05 19:03

    Some browsers do not have console enabled when the dev-tools is closed. Also, one would encounter this issue with WebViews or iFrames where console is disabled.

    The error in these cases is - Uncaught ReferenceError: console is not defined

    Inspired by many answers on here, I developed a library for this usecase: https://github.com/sunnykgupta/jsLogger

    Features:

    1. It safely overrides the console.log.
    2. Takes care if the console is not available (oh yes, you need to factor that too.)
    3. Stores all logs (even if they are suppressed) for later retrieval.
    4. Handles major console functions like log, warn, error, info.
    0 讨论(0)
提交回复
热议问题