Which browsers support console.log()?

后端 未结 8 1537
生来不讨喜
生来不讨喜 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/

提交回复
热议问题