Which browsers support console.log()?

后端 未结 8 1515
生来不讨喜
生来不讨喜 2020-12-03 13:12

Do all browsers support this? I would like to output an error using console.log() but was wondering if this supported by all browsers?

console.         


        
相关标签:
8条回答
  • 2020-12-03 14:15

    I've done something like this in the past:

    // Log to native console if possible, alert otherwise
    window.console = typeof window.console === 'undefined'
        ? {log:function(/* polymorphic */){alert(arguments)}}
        : window.console;
    

    You can put that at the "top" of your JS and it works pretty nicely when you're forced to do debugging w/ a browser that doesn't support console, and doesn't require you to change your other JS source that's already calling console.log all over the place. Of course you might want to do something other than alert to preserve your sanity...

    http://jsfiddle.net/4dty5/

    0 讨论(0)
  • 2020-12-03 14:17

    Here is a workaround for when console.log() is not available. You have to retrieve the console.logs yourself.

      if (!window.console) window.console = {};
      if (!window.console.log) 
      {
        window.console.logs = [];
        window.console.log = function (string)
        {
          window.console.logs.push(string);
        };
      }
    
    0 讨论(0)
提交回复
热议问题