javascript 在 IE 中写入脚本控制台 (console.log) 的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4324454/
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
What's the correct way to write to the script console (console.log) in IE?
提问by Harry Bennett
I have this substitute for console.logdefined in document.ready():
我有这个替代品console.log定义在document.ready():
$(document).ready(function(){
console.log("doc ready");
if(typeof console === "undefined"){
console = { log: function() { } };
}
}
I thought IE was supposed to have this function available but, when I include the call above
我以为 IE 应该有这个功能,但是,当我包含上面的调用时
console.log("doc ready");
the output appears in the Firefox console but not in IE - in fact IE script execution breaks completely at this point.
输出出现在 Firefox 控制台中,但不在 IE 中 - 实际上此时 IE 脚本执行完全中断。
What's the correct way to write to the console in IE?
在 IE 中写入控制台的正确方法是什么?
回答by Dr.Molle
The script-execution breaks because of wrong order of the instructions, this may be better:
由于指令顺序错误,脚本执行中断,这可能更好:
$(document).ready(function(){
if(typeof console === "undefined"){
console = { log: function() { } };
}
console.log("doc ready");
}
If you first access the console before checking if it exists(and creating it if not), this results into an error.
如果您在检查控制台是否存在(如果不存在则创建它)之前首先访问控制台,这会导致错误。
回答by Spudley
IE6/7 doesn't have a console by default.
IE6/7 默认没有控制台。
In fact, neither does Firefox -- it is provided by a plug-in called Firebug; if you use a copy of Firefox without Firebug installed, then you'll get errors trying to call consolejust the same as with IE.
事实上,Firefox 也没有——它是由一个名为 Firebug 的插件提供的;如果您使用未安装 Firebug 的 Firefox 副本,那么您将在尝试调用时遇到console与 IE 相同的错误。
IE8/9 dohave a console.
IE8/9确实有一个控制台。
Chrome and Safari do have a built-in console object, but don't count on it working in exactly the same way as Firebug or IE8.
Chrome 和 Safari 确实有一个内置的控制台对象,但不要指望它的工作方式与 Firebug 或 IE8 完全相同。
Note that in all browsers, the console object may not be created unless the debug window is open. This means your code with a console.logcall could fail in anybrowser, not just IE.
请注意,在所有浏览器中,除非打开调试窗口,否则可能无法创建控制台对象。这意味着您的console.log调用代码可能会在任何浏览器中失败,而不仅仅是 IE。
In your example, you are essentially creating a dummy consoleobject if it doesn't exist, which is clearly intended to prevent browsers without a console from crashing if you call console.log(). But you're calling console.log()before that code is run, so those browsers without a console will crash on that line. You should therefore move your console.log("doc ready");line down so it comes after the bit that detects whether consoleexists.
在您的示例中,您实际上是在创建一个console不存在的虚拟对象,这显然是为了防止没有控制台的浏览器在您调用console.log(). 但是您console.log()在该代码运行之前调用,因此那些没有控制台的浏览器将在该行崩溃。因此,您应该将console.log("doc ready");行向下移动,使其位于检测是否console存在的位之后。
If you want the console to exist for IE, there is a version of Firebug called Firebug Lite, which can be run on any browser. If you run this, it will create the consoleobject.
如果您希望控制台存在于 IE 中,则有一个名为Firebug Lite的 Firebug 版本,它可以在任何浏览器上运行。如果你运行它,它将创建console对象。
However, note that it can only be run after the page has loaded, so you'll never be able to get it to show console messages in the document ready function. Additionally, it may fail to create the console object if it already exists, so the code you have in document ready to create a dummy console object may prevent Firebug Lite from working correctly.
但是,请注意它只能在页面加载后运行,因此您永远无法让它在文档就绪函数中显示控制台消息。此外,如果控制台对象已经存在,它可能无法创建控制台对象,因此您在文档中准备创建虚拟控制台对象的代码可能会阻止 Firebug Lite 正常工作。
Finally, while using the console for is fantastic for debugging purposes, please make sure you never ship live code with calls to console.log, even if you plan to only use them for debugging purposes later. As you've seen already, they can cause a browser to stop executing the code if it doesn't have a console object, and there will be plenty of live users who don't have it, so beware of causing issues for live users: the best thing is to always ensure you've removed all your calls to the console before shipping your code.
最后,虽然使用控制台 for 非常适合用于调试目的,但请确保您永远不会发送带有调用 的实时代码console.log,即使您打算稍后仅将它们用于调试目的。正如你已经看到的,如果浏览器没有控制台对象,它们会导致浏览器停止执行代码,并且会有很多没有它的在线用户,所以要小心给在线用户带来问题:最好的办法是始终确保在发送代码之前已删除对控制台的所有调用。
回答by Ollie Edwards
Here's what I use to failover to firebug lite if there is no console available. This guarantees you'll get console of some description although they all work slightly differently so be wary.
如果没有可用的控制台,这就是我用来故障转移到 firebug lite 的方法。这保证你会得到一些描述的控制台,尽管它们的工作方式都略有不同,所以要小心。
function attachConsole(force) {
if(force || typeof console === "undefined"){
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js';
head.appendChild(script);
return true;
}
return false;
}
回答by rahul
console is for firebug.
控制台用于萤火虫。
You will have to install firebug liteto enable writing to console in IE.
您必须安装firebug lite才能在 IE 中写入控制台。

