我使用Firebug,并有一些语句像:
console.log("...");
在我的页面上。在IE8(可能是更早的版本),我得到脚本错误说“控制台”是未定义的。我试着把这个放在我页面的顶部:
<script type="text/javascript">
if (!console) console = {log: function() {}};
</script>
我还是会得到错误。有办法消除错误吗?
我使用Firebug,并有一些语句像:
console.log("...");
在我的页面上。在IE8(可能是更早的版本),我得到脚本错误说“控制台”是未定义的。我试着把这个放在我页面的顶部:
<script type="text/javascript">
if (!console) console = {log: function() {}};
</script>
我还是会得到错误。有办法消除错误吗?
当前回答
为了更健壮的解决方案,使用下面的代码(取自twitter的源代码):
// 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(),也可以使用脚本选项卡上的控制台文本框。
Try
if (!window.console) console = ...
未定义的变量不能直接引用。但是,所有全局变量都是全局上下文(浏览器为窗口)同名的属性,访问未定义的属性是可以的。
或者使用if (typeof console === 'undefined') console =…如果你想避开神奇的变量窗口,请参阅@Tim Down的回答。
在有这么多问题之后(很难调试错误,因为如果你打开开发人员控制台,错误就不再发生了!)我决定做一个多余的代码,这样就再也不用为这个问题烦恼了:
if (typeof window.console === "undefined")
window.console = {};
if (typeof window.console.debug === "undefined")
window.console.debug= function() {};
if (typeof window.console.log === "undefined")
window.console.log= function() {};
if (typeof window.console.error === "undefined")
window.console.error= function() {alert("error");};
if (typeof window.console.time === "undefined")
window.console.time= function() {};
if (typeof window.console.trace === "undefined")
window.console.trace= function() {};
if (typeof window.console.info === "undefined")
window.console.info= function() {};
if (typeof window.console.timeEnd === "undefined")
window.console.timeEnd= function() {};
if (typeof window.console.group === "undefined")
window.console.group= function() {};
if (typeof window.console.groupEnd === "undefined")
window.console.groupEnd= function() {};
if (typeof window.console.groupCollapsed === "undefined")
window.console.groupCollapsed= function() {};
if (typeof window.console.dir === "undefined")
window.console.dir= function() {};
if (typeof window.console.warn === "undefined")
window.console.warn= function() {};
我个人只使用console.log和console。错误,但这段代码处理所有其他函数显示在Mozzila开发者网络:https://developer.mozilla.org/en-US/docs/Web/API/console。 只要把代码放在页面的顶部,你就永远不用这样做了。
在TypeScript中控制台的存根:
if (!window.console) {
console = {
assert: () => undefined,
clear: () => undefined,
count: () => undefined,
debug: () => undefined,
dir: () => undefined,
dirxml: () => undefined,
error: () => undefined,
group: () => undefined,
groupCollapsed: () => undefined,
groupEnd: () => undefined,
info: () => undefined,
log: () => undefined,
msIsIndependentlyComposed: (e: Element) => false,
profile: () => undefined,
profileEnd: () => undefined,
select: () => undefined,
time: () => undefined,
timeEnd: () => undefined,
trace: () => undefined,
warn: () => undefined,
}
};
要在IE中调试,请查看这个log4javascript