Javascript 哪些浏览器支持 console.log()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14086675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which browsers support console.log()?
提问by KingKongFrog
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");
回答by Aaron McIver
No, not all browsers support console.logas it is not part of the standard and is an extension of the DOM and thus you should not count on its presence. To make your code resilient you should assume it does notexist and code accordingly.
不,并非所有浏览器都支持,console.log因为它不是标准的一部分,而是 DOM 的扩展,因此您不应指望它的存在。为了使您的代码具有弹性,您应该假设它不存在并相应地编码。
回答by Madbreaks
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.logall over the place. Of course you might want to do something other than alertto preserve your sanity...
你可以把它放在你的 JS 的“顶部”,当你被迫使用不支持的浏览器进行调试时它工作得非常好console,并且不需要你更改已经调用的其他 JS 源console.log到处都是。当然,除了alert保持理智之外,您可能还想做点什么……
回答by joeytwiddle
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.(caniuse)
移动和桌面上的所有主要浏览器都支持 console.log。( 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.consoleexists.It might not work in WebWorkerson all browsers. (MDN)
In UC Browser and Opera Mini the functions will run, but you won't see the output. (caniuse)
如果您需要支持旧浏览器 ( IE<10)、更多移动浏览器或运行实验浏览器的用户,那么使用 polyfill ( 1, 2, 3, 4) 来确保
window.console存在可能是个好主意。它可能不适用于所有浏览器的WebWorkers。( MDN)
在 UC 浏览器和 Opera Mini 中,这些功能将运行,但您不会看到输出。( caniuse)
But for the vast majority of web users now, we can assume console.logwill work as expected.
但是对于现在绝大多数的网络用户来说,我们可以假设console.log会按预期工作。
回答by asgoth
Make a wrapper function:
制作一个包装函数:
function log(text) {
if (window.console) {
window.console.log(text);
}
}
回答by ow3n
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 () { };
回答by Menztrual
Most browsers do, however Internet Explorer 9 has issues with it where it wont run any javascript unless you have the debug window open. Took us hours to find the solution to that problem.
大多数浏览器都可以,但是 Internet Explorer 9 存在问题,除非您打开调试窗口,否则它不会运行任何 javascript。我们花了几个小时才找到该问题的解决方案。
回答by Sprout Coder
Here is a workaround for when console.log()is not available. You have to retrieve the console.logsyourself.
这是何时console.log()不可用的解决方法。你必须console.logs自己找回。
if (!window.console) window.console = {};
if (!window.console.log)
{
window.console.logs = [];
window.console.log = function (string)
{
window.console.logs.push(string);
};
}
回答by ambodi
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.logissues. He explains that he would like to have a log message which works with all browsers and here is how he coded it:
John Resig(jQuery 的创建者)在他的“Javascript Ninja 的秘密”一书中有一个非常简单的代码,可以处理跨浏览器console.log问题。他解释说他想要一条适用于所有浏览器的日志消息,这是他的编码方式:
function log() {
try {
console.log.apply(console, arguments);
} catch(e) {
try {
opera.postError.apply(opera, arguments);
}
catch(e) {
alert(Array.prototype.join.call( arguments, " "));
}
}

