lee*_*d00 252 javascript console logging internet-explorer-8
根据这篇文章它是在测试版,但它不是在发布?
Mis*_*cky 257
console.log仅在您打开开发人员工具(F12以打开和关闭它)后才可用.有趣的是,在你打开它之后,你可以关闭它,然后仍然通过console.log调用发送到它,当你重新打开它时会看到它们.我认为这是一个各种各样的错误,可能会修复,但我们会看到.
我可能只是使用这样的东西:
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)
}
Run Code Online (Sandbox Code Playgroud)
甚至更简单:
function trace(s) {
try { console.log(s) } catch (e) { alert(s) }
}
Run Code Online (Sandbox Code Playgroud)
jps*_*ain 228
对于后备更好的是:
var alertFallback = true;
if (typeof console === "undefined" || typeof console.log === "undefined") {
console = {};
if (alertFallback) {
console.log = function(msg) {
alert(msg);
};
} else {
console.log = function() {};
}
}
Wal*_*osz 56
这是我对各种答案的看法.我想实际看到记录的消息,即使我没有在它们被触发时打开IE控制台,所以我将它们推入console.messages我创建的数组中.我还添加了一个功能,console.dump()以方便查看整个日志.console.clear()将清空消息队列.
此解决方案还"处理"其他控制台方法(我相信所有这些方法都来自Firebug控制台API)
最后,该解决方案采用IIFE形式,因此不会污染全球范围.fallback函数参数在代码的底部定义.
我把它放在我的主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)
Run Code Online (Sandbox Code Playgroud)
该行var args = Array.prototype.slice.call(arguments);从argumentsObject 创建一个Array .这是必需的,因为参数实际上不是数组.
trap()是任何API函数的默认处理程序.我传递参数,message以便获得传递给任何API调用的参数的日志(不仅仅是console.log).
我添加了一个额外的数组console.raw,捕获完全传递给的参数trap().我意识到args.join(' ')将对象转换为字符串"[object Object]"有时可能是不受欢迎的.感谢bfontaine的建议.
Jam*_*are 52
值得注意的是,console.log在IE8中并不是一个真正的Javascript函数.它不支持apply或call方法.
Lei*_*and 44
假设您不关心回退警报,这里有一个更简洁的方法来解决Internet Explorer的缺点:
var console=console||{"log":function(){}};
Run Code Online (Sandbox Code Playgroud)
Chr*_*cki 25
我非常喜欢"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() {}; }
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
如果你对所有的console.log调用都"未定义",那可能意味着你仍然加载了一个旧的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;
}
}
}());
Run Code Online (Sandbox Code Playgroud)