I have this substitute for console.log
defined in document.ready()
:
我有这个替换在document.ready()中定义的console.log:
$(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中写入控制台的正确方法是什么?
4 个解决方案
#1
13
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.
如果您首先访问控制台,然后检查它是否存在(如果不存在则创建它),这会导致错误。
#2
6
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 console
just the same as with IE.
实际上,Firefox也没有 - 它是由一个名为Firebug的插件提供的;如果您使用没有安装Firebug的Firefox副本,那么尝试调用控制台时会出现与IE相同的错误。
IE8/9 do have 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.log
call could fail in any browser, not just IE.
请注意,在所有浏览器中,除非打开调试窗口,否则可能无法创建控制台对象。这意味着使用console.log调用的代码可能在任何浏览器中失败,而不仅仅是IE。
In your example, you are essentially creating a dummy console
object 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 console
exists.
在您的示例中,您实际上创建了一个虚拟控制台对象(如果它不存在),这显然是为了防止没有控制台的浏览器在调用console.log()时崩溃。但是在运行该代码之前你正在调用console.log(),所以那些没有控制台的浏览器会在该行上崩溃。因此,您应该移动console.log(“doc ready”);在检测控制台是否存在的位之后会出现这种情况。
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 console
object.
如果你想让IE控制台存在,那么有一个名为Firebug Lite的Firebug版本可以在任何浏览器上运行。如果运行它,它将创建控制台对象。
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.
最后,虽然使用控制台非常适合调试,但请确保您从未发送调用console.log的实时代码,即使您计划以后仅将其用于调试目的。正如您已经看到的那样,如果没有控制台对象,它们可能导致浏览器停止执行代码,并且会有大量没有它的实时用户,因此请注意为实时用户造成问题:最好的办法是始终确保在发送代码之前已删除了对控制台的所有呼叫。
#3
2
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;
}
#4
0
console is for firebug.控制台是用于萤火虫。
You will have to install firebug lite to enable writing to console in IE.
您必须安装firebug lite才能在IE中写入控制台。
#1
13
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.
如果您首先访问控制台,然后检查它是否存在(如果不存在则创建它),这会导致错误。
#2
6
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 console
just the same as with IE.
实际上,Firefox也没有 - 它是由一个名为Firebug的插件提供的;如果您使用没有安装Firebug的Firefox副本,那么尝试调用控制台时会出现与IE相同的错误。
IE8/9 do have 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.log
call could fail in any browser, not just IE.
请注意,在所有浏览器中,除非打开调试窗口,否则可能无法创建控制台对象。这意味着使用console.log调用的代码可能在任何浏览器中失败,而不仅仅是IE。
In your example, you are essentially creating a dummy console
object 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 console
exists.
在您的示例中,您实际上创建了一个虚拟控制台对象(如果它不存在),这显然是为了防止没有控制台的浏览器在调用console.log()时崩溃。但是在运行该代码之前你正在调用console.log(),所以那些没有控制台的浏览器会在该行上崩溃。因此,您应该移动console.log(“doc ready”);在检测控制台是否存在的位之后会出现这种情况。
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 console
object.
如果你想让IE控制台存在,那么有一个名为Firebug Lite的Firebug版本可以在任何浏览器上运行。如果运行它,它将创建控制台对象。
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.
最后,虽然使用控制台非常适合调试,但请确保您从未发送调用console.log的实时代码,即使您计划以后仅将其用于调试目的。正如您已经看到的那样,如果没有控制台对象,它们可能导致浏览器停止执行代码,并且会有大量没有它的实时用户,因此请注意为实时用户造成问题:最好的办法是始终确保在发送代码之前已删除了对控制台的所有呼叫。
#3
2
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;
}
#4
0
console is for firebug.控制台是用于萤火虫。
You will have to install firebug lite to enable writing to console in IE.
您必须安装firebug lite才能在IE中写入控制台。