根据这篇文章,它在测试版中,但它不在发行版中?
当前回答
答案太多了。我的解决方案是:
globalNamespace.globalArray = new Array();
if (typeof console === "undefined" || typeof console.log === "undefined") {
console = {};
console.log = function(message) {globalNamespace.globalArray.push(message)};
}
简而言之,如果console.log不存在(或者在本例中没有打开),则将日志存储在全局名称空间数组中。这样,您就不会受到数百万条警报的困扰,并且仍然可以在打开或关闭开发人员控制台的情况下查看日志。
其他回答
以下是我对各种答案的看法。我想真正看到记录的消息,即使它们被触发时我没有打开IE控制台,所以我把它们推到一个控制台。我创建的消息数组。我还添加了一个console.dump()函数,以方便查看整个日志。Console.clear()将清空消息队列。
这个解决方案还“处理”了其他控制台方法(我相信它们都起源于Firebug控制台API)
最后,该解决方案采用IIFE形式,因此不会污染全局范围。回退函数参数在代码的底部定义。
我只是把它放在我的主JS文件中,包括在每一页,然后忘记它。
(function (fallback) {
fallback = fallback || function () { };
// function to trap most of the console functions from the FireBug Console API.
var trap = function () {
// create an Array from the arguments Object
var args = Array.prototype.slice.call(arguments);
// console.raw captures the raw args, without converting toString
console.raw.push(args);
var message = args.join(' ');
console.messages.push(message);
fallback(message);
};
// redefine console
if (typeof console === 'undefined') {
console = {
messages: [],
raw: [],
dump: function() { return console.messages.join('\n'); },
log: trap,
debug: trap,
info: trap,
warn: trap,
error: trap,
assert: trap,
clear: function() {
console.messages.length = 0;
console.raw.length = 0 ;
},
dir: trap,
dirxml: trap,
trace: trap,
group: trap,
groupCollapsed: trap,
groupEnd: trap,
time: trap,
timeEnd: trap,
timeStamp: trap,
profile: trap,
profileEnd: trap,
count: trap,
exception: trap,
table: trap
};
}
})(null); // to define a fallback function, replace null with the name of the function (ex: alert)
一些额外的信息
line var args = Array.prototype.slice.call(参数);从参数Object创建一个数组。这是必需的,因为arguments并不是真正的数组。
trap()是任何API函数的默认处理程序。我将参数传递给message,以便您获得传递给任何API调用的参数的日志(不仅仅是console.log)。
Edit
我增加了一个额外的数组控制台。捕获传递给trap()的参数。我意识到args。join(' ')正在将对象转换为字符串"[object object]",这有时可能是不可取的。谢谢你的建议。
这是我的“IE,请不要崩溃”
typeof console=="undefined"&&(console={});typeof console.log=="undefined"&&(console.log=function(){});
如果你所有的console.log调用都是“undefined”,这可能意味着你仍然加载了一个旧的firebuglite (firebug.js)。它将覆盖IE8的console.log的所有有效函数,即使它们确实存在。这就是发生在我身上的事。
检查重写控制台对象的其他代码。
对于没有控制台的浏览器来说,最好的解决方案是:
// 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;
}
}
}());
值得注意的是,IE8中的console.log并不是一个真正的Javascript函数。它不支持apply或call方法。
推荐文章
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?
- 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?
- 如何使用JavaScript重新加载ReCaptcha ?
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误