Javascript 查看控制台是否可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4946287/
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
finding out if console is available
提问by helle
I was wondering, how can i find out with javascriptif the console object is available?
我想知道,如果控制台对象可用,我怎么能用javascript找出?
i have the problem that if i forget to remove a debug output like console.log('sthg')
i get errors in several browsers, if there is no firebug, or similar, active.
我有一个问题,如果我忘记删除调试输出,就像console.log('sthg')
我在几个浏览器中遇到错误一样,如果没有萤火虫或类似的活动。
thanks for help
感谢帮助
next to that problem i am interested in all informations about the console object. has anybody some documentation link, or so? is it a standard? and so on...
在那个问题旁边,我对有关控制台对象的所有信息感兴趣。有人有一些文档链接吗?它是一个标准吗?等等...
回答by Andy E
Check the property exists as a member of window
:
检查该属性是否作为 的成员存在window
:
if (window.console) {
}
next to that problem i am interested in all informations about the console object. has anybody some documentation link, or so? is it a standard? and so on...
在那个问题旁边,我对有关控制台对象的所有信息感兴趣。有人有一些文档链接吗?它是一个标准吗?等等...
Check out the Firebug documentation for the Console API; Chrome and Safari implement most, but not all, of the methods listed there. There's no standard defining what should be in the console, so you'll need to test each browser to see if it supports the feature.
查看控制台 API的Firebug 文档;Chrome 和 Safari 实现了其中列出的大部分方法,但不是全部。没有标准定义控制台中应该包含的内容,因此您需要测试每个浏览器以查看它是否支持该功能。
回答by Matthew O'Riordan
A nice simple and short way of outputting to the console safely is as follows:
安全输出到控制台的一种简单而简短的方法如下:
window.console && console.log('Debug message');
回答by code_monk
here's what i use. bear in mind that i am only half-heartedly supporting browsers with no support for console. and i only ever use console.log(), but you can see how it can be extended to support console.dir(), console.info(), etc
这是我使用的。请记住,我只是半心半意地支持不支持控制台的浏览器。我只使用过console.log(),但你可以看到它如何扩展以支持console.dir()、console.info()等
var console = console || {
"log": function(stuff) {}
};
I like it because calling it does not cause an error, but it returns [undefined], which i think is appropriate.
我喜欢它,因为调用它不会导致错误,但它返回 [undefined],我认为这是合适的。
Note that many many people before (and after) us have written similar polyfills:
请注意,在我们之前(和之后)有很多人写过类似的 polyfill:
回答by Robby Pond
Defined by firebug, IE8 (need to open the developer tools with F12), Chrome, etc but there is no defined spec for it. There is a console.log wrapperthat makes it a very easy to use, cross browser logging solution so if the console doesn't exist your code doesn't explode.
由firebug、IE8(需要用 F12 打开开发者工具)、Chrome 等定义,但没有定义规范。有一个console.log 包装器,使它成为一个非常易于使用的跨浏览器日志记录解决方案,因此如果控制台不存在,您的代码不会爆炸。
回答by Nadeem Yasin
try{
console.log("test")
}
catch(e){
console={},
console.log=function(a){}
}
just put it at the top of your JS file and then use console.log(); without any worry for browser error, i also had this error in IE9
把它放在你的 JS 文件的顶部,然后使用 console.log(); 不用担心浏览器错误,我在 IE9 中也有这个错误
回答by Bas van Ommen
I always include this in the top of my HTML-header before I load anything else. Debugging with console.debug is just too long for me. And I like to toggle the usage of these console functions.
在加载其他任何内容之前,我总是将它包含在我的 HTML 标题的顶部。使用 console.debug 调试对我来说太长了。我喜欢切换这些控制台功能的使用。
Don't know how optimized the code is, but it always does the job.
不知道代码是如何优化的,但它总是可以完成工作。
(function() {
var consoleDisabled = false;
if (consoleDisabled) {
window.console = undefined;
}
if (window.console == undefined) {
window.console = {
debug: function() {
return true;
},
info: function() {
return false;
},
warn: function() {
return false;
},
log: function() {
return false;
}
}
}
debug = (function(args) {
window.console.debug(args);
});
info = (function(args) {
window.console.info(args);
});
warn = (function(args) {
window.console.warn(args);
});
log = (function(args) {
window.console.log(args);
});
})();
debug(somevar);
info(somevar);
warn(somevar);
log(somevar);
回答by Asish Sinha
Simplest way would be:
最简单的方法是:
if (window.console){
console.log('do something');
}
instead of just writing:
而不是只写:
console.log('do something');
回答by Syl
maybe...
也许...
if (console) {
// do stuff
}