简体   繁体   English

IE9是否支持console.log,它是真正的功能吗?

[英]Does IE9 support console.log, and is it a real function?

In which circumstances is window.console.log defined in Internet Explorer 9? 在哪种情况下,Internet Explorer 9中定义了window.console.log

Even when window.console.log is defined, window.console.log.apply and window.console.log.call are undefined. 即使window.console.log定义, window.console.log.applywindow.console.log.call是不确定的。 Why is this? 为什么是这样?

[Related question for IE8: What happened to console.log in IE8? [IE8的相关问题:IE8 中的console.log发生了什么变化? .] ]

In Internet Explorer 9 (and 8), the console object is only exposed when the developer tools are opened for a particular tab. 在Internet Explorer 9(和8)中,只有在为特定选项卡打开开发人员工具时才会公开console对象。 If you hide the developer tools window for that tab, the console object remains exposed for each page you navigate to. 如果隐藏该选项卡的开发人员工具窗口,则对于您导航到的每个页面, console对象将保持公开状态。 If you open a new tab, you must also open the developer tools for that tab in order for the console object to be exposed. 如果打开新选项卡,还必须打开该选项卡的开发人员工具,以便公开console对象。

The console object is not part of any standard and is an extension to the Document Object Model. console对象不是任何标准的一部分,是文档对象模型的扩展。 Like other DOM objects, it is considered a host object and is not required to inherit from Object , nor its methods from Function , like native ECMAScript functions and objects do. 与其他DOM对象一样,它被认为是一个宿主对象,不需要从Object继承,也不需要从Function继承它的方法,就像本机ECMAScript函数和对象一样。 This is the reason apply and call are undefined on those methods. 这就是applycall未定义的原因。 In IE 9, most DOM objects were improved to inherit from native ECMAScript types. 在IE 9中,大多数DOM对象都得到了改进,可以从本机ECMAScript类型继承。 As the developer tools are considered an extension to IE (albeit, a built-in extension), they clearly didn't receive the same improvements as the rest of the DOM. 由于开发人员工具被认为是IE的扩展(虽然是内置扩展),但他们显然没有得到与其他DOM相同的改进。

For what it's worth, you can still use some Function.prototype methods on console methods with a little bind() magic: 对于它的价值,您仍然可以在console方法上使用一些具有一点bind()魔法的Function.prototype方法:

var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);
//-> "thisisatest"

A simple solution to this console.log problem is to define the following at the beginning of your JS code: 这个console.log问题的一个简单解决方案是在JS代码的开头定义以下内容:

if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };

This works for me in all browsers. 这适用于所有浏览器。 This creates a dummy function for console.log when the debugger is not active. 当调试器未激活时,这将为console.log创建一个虚函数。 When the debugger is active, the method console.log is defined and executes normally. 当调试器处于活动状态时,方法console.log已定义并正常执行。

I know this is a very old question but feel this adds a valuable alternative of how to deal with the console issue. 我知道这是一个非常古老的问题但是觉得这增加了如何处理控制台问题的有价值的替代方案。 Place the following code before any call to console.* (so your very first script). 在调用console。之前放置以下代码。*(所以你的第一个脚本)。

// 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;
        }
    }
}());

Reference: 参考:
https://github.com/h5bp/html5-boilerplate/blob/v5.0.0/dist/js/plugins.js https://github.com/h5bp/html5-boilerplate/blob/v5.0.0/dist/js/plugins.js

console.log is only defined when the console is open. console.log仅在控制台打开时定义。 If you want to check for it in your code make sure you check for for it within the window property 如果要在代码中检查它,请确保在窗口属性中检查它

if (window.console)
    console.log(msg)

this throws an exception in IE9 and will not work correctly. 这会在IE9中引发异常,无法正常工作。 Do not do this 不要这样做

if (console) 
    console.log(msg)

After reading the article from Marc Cliament's comment above, I've now changed my all-purpose cross-browser console.log function to look like this: 在阅读了Marc Cliament上面评论的文章之后,我现在将我的通用跨浏览器console.log函数更改为如下所示:

function log()
{
    "use strict";

    if (typeof(console) !== "undefined" && console.log !== undefined)
    {
        try
        {
            console.log.apply(console, arguments);
        }
        catch (e)
        {
            var log = Function.prototype.bind.call(console.log, console);
            log.apply(console, arguments);
        }
    }
}

I would like to mention that IE9 does not raise the error if you use console.log with developer tools closed on all versions of Windows. 我想提一下,如果您使用console.log并在所有版本的Windows上关闭开发人员工具,IE9不会引发错误。 On XP it does, but on Windows 7 it doesn't. 在XP上它确实如此,但在Windows 7上却没有。 So if you dropped support for WinXP in general, you're fine using console.log directly. 因此,如果你放弃了对WinXP的支持,你可以直接使用console.log。

怎么样...

console = { log : function(text) { alert(text); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM