简体   繁体   English

哪些浏览器支持console.log()?

[英]Which browsers support console.log()?

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.log()输出错误但是想知道这是否支持所有浏览器?

console.log("Error etc");

No, not all browsers support console.log as it is not part of the standard and is an extension of the DOM and thus you should not count on its presence. 不,并非所有浏览器都支持console.log因为它不是标准的一部分,并且是DOM的扩展,因此您不应指望它的存在。 To make your code resilient you should assume it does not exist and code accordingly. 为了使您的代码具有弹性,您应该假设它存在并相应地编码。

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. 您可以将它放在JS的“顶部”,当您被迫使用不支持console的浏览器进行调试时,它可以很好地工作,并且不需要您更改已经存在的其他JS源代码在整个地方调用console.log Of course you might want to do something other than alert to preserve your sanity... 当然,你可能想做一些alert以外的事情,以保持你的理智......

http://jsfiddle.net/4dty5/ http://jsfiddle.net/4dty5/

It is now August 2015 , and I think the current answer to this question is: 现在是2015年8月 ,我认为目前这个问题的答案是:

All major browsers on mobile and desktop support console.log. 移动和桌面上的所有主流浏览器都支持console.log。 ( caniuse ) caniuse

It is not part of any standard, but it is now an expected deliverable of a complete modern browser. 它不是任何标准的一部分,但它现在是完整的现代浏览器的预期可交付成果。

However: 然而:

If you need to support old browsers ( IE<10 ), more mobile browsers, or users running experimental browsers, then it might be a good idea to use a polyfill ( 1 , 2 , 3 , 4 ) to ensure window.console exists. 如果您需要支持旧的浏览器( IE <10 ),更多的移动浏览器,或用户运行实验的浏览器,那么它可能是使用填充工具(一个好主意, 1234 ),以确保window.console存在。

It might not work in WebWorkers on all browsers. 它可能无法在所有浏览器上的WebWorkers中使用。 ( MDN ) MDN

In UC Browser and Opera Mini the functions will run, but you won't see the output. 在UC浏览器和Opera Mini中,功能将运行,但您将看不到输出。 ( caniuse ) caniuse

But for the vast majority of web users now, we can assume console.log will work as expected. 但对于绝大多数网络用户来说,我们可以假设console.log将按预期工作。

Make a wrapper function: 制作包装函数:

function log(text) {
  if (window.console) {
     window.console.log(text);
  }
}

Most browsers do, however Internet Explorer 9 has issues with it where it wont run any javascript unless you have the debug window open. 大多数浏览器都这样做,但Internet Explorer 9存在问题,除非你打开调试窗口,否则它不会运行任何javascript。 Took us hours to find the solution to that problem. 花了我们几个小时来找到解决这个问题的方法。

This code will check to see if the function exists and create a dummy in case it does not. 此代码将检查函数是否存在,并在不存在的情况下创建虚拟函数。 Credit: StackOverflow 信用: StackOverflow

if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };

Here is a workaround for when console.log() is not available. 以下是console.log()不可用时的解决方法。 You have to retrieve the console.logs yourself. 您必须自己检索console.logs

  if (!window.console) window.console = {};
  if (!window.console.log) 
  {
    window.console.logs = [];
    window.console.log = function (string)
    {
      window.console.logs.push(string);
    };
  }

Although not all browsers support that, it can be accomplished with a small chunk of code. 虽然并非所有浏览器都支持这种功能,但可以使用一小块代码来完成。

In his book, "Secrets of Javascript Ninja", John Resig (creator of jQuery) has a really simple code which will handle cross-browser console.log issues. 在他的书“Javascript Ninja的秘密”中,John Resig(jQuery的创建者)有一个非常简单的代码,可以处理跨浏览器的console.log问题。 He explains that he would like to have a log message which works with all browsers and here is how he coded it: 他解释说,他希望有一条适用于所有浏览器的日志消息,以下是他编码的方式:

 function log() {
  try {
     console.log.apply(console, arguments);
  } catch(e) {
  try {
     opera.postError.apply(opera, arguments);
  }
  catch(e) {
     alert(Array.prototype.join.call( arguments, " "));
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM