根据这篇文章,它在测试版中,但它不在发行版中?
当前回答
console.log只有在打开开发人员工具(F12来切换它的打开和关闭)后才可用。 有趣的是,在你打开它之后,你可以关闭它,然后仍然通过console.log调用发布到它,这些将在你重新打开它时看到。 我认为这是一个bug,可能会被修复,但我们将拭目以待。
我可能会用这样的东西:
function trace(s) {
if ('console' in self && 'log' in console) console.log(s)
// the line below you might want to comment out, so it dies silent
// but nice for seeing when the console is available or not.
else alert(s)
}
更简单的是:
function trace(s) {
try { console.log(s) } catch (e) { alert(s) }
}
其他回答
如果你所有的console.log调用都是“undefined”,这可能意味着你仍然加载了一个旧的firebuglite (firebug.js)。它将覆盖IE8的console.log的所有有效函数,即使它们确实存在。这就是发生在我身上的事。
检查重写控制台对象的其他代码。
我喜欢这个方法(使用jquery的doc ready)…它可以让你使用控制台甚至在ie…唯一的问题是,如果你在页面加载后打开ie的开发工具,你需要重新加载页面……
如果把所有的函数都考虑进去,可能会更圆滑一些,但我只使用log,这就是我要做的。
//one last double check against stray console.logs
$(document).ready(function (){
try {
console.log('testing for console in itcutils');
} catch (e) {
window.console = new (function (){ this.log = function (val) {
//do nothing
}})();
}
});
我真的很喜欢“orange80”发布的方法。它很优雅,因为你可以设置一次,然后忘记它。
其他方法要求您做一些不同的事情(每次调用console.log()以外的东西),这只是在自找麻烦……我知道我最终会忘记。
我更进一步,通过将代码包装在一个实用函数中,您可以在javascript的开头调用一次,只要是在任何日志记录之前。(我正在我公司的事件数据路由器产品中安装这个。这将有助于简化新管理界面的跨浏览器设计。)
/**
* Call once at beginning to ensure your app can safely call console.log() and
* console.dir(), even on browsers that don't support it. You may not get useful
* logging on those browers, but at least you won't generate errors.
*
* @param alertFallback - if 'true', all logs become alerts, if necessary.
* (not usually suitable for production)
*/
function fixConsole(alertFallback)
{
if (typeof console === "undefined")
{
console = {}; // define it if it doesn't exist already
}
if (typeof console.log === "undefined")
{
if (alertFallback) { console.log = function(msg) { alert(msg); }; }
else { console.log = function() {}; }
}
if (typeof console.dir === "undefined")
{
if (alertFallback)
{
// THIS COULD BE IMPROVED… maybe list all the object properties?
console.dir = function(obj) { alert("DIR: "+obj); };
}
else { console.dir = function() {}; }
}
}
下面是一个版本,当开发人员工具打开而不是关闭时,它将登录到控制台。
(function(window) {
var console = {};
console.log = function() {
if (window.console && (typeof window.console.log === 'function' || typeof window.console.log === 'object')) {
window.console.log.apply(window, arguments);
}
}
// Rest of your application here
})(window)
对于没有控制台的浏览器来说,最好的解决方案是:
// Avoid `console` errors in browsers that lack a console.
(function() {
var method;
var noop = function () {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeStamp', 'trace', 'warn'
];
var length = methods.length;
var console = (window.console = window.console || {});
while (length--) {
method = methods[length];
// Only stub undefined methods.
if (!console[method]) {
console[method] = noop;
}
}
}());
推荐文章
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类
- 访问Handlebars.js每次循环范围之外的变量
- 如何用JavaScript截屏一个div ?
- 如何为其他域设置cookie