Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IE8中的console.log发生了什么?_Javascript_Logging_Internet Explorer 8_Console - Fatal编程技术网

Javascript IE8中的console.log发生了什么?

Javascript IE8中的console.log发生了什么?,javascript,logging,internet-explorer-8,console,Javascript,Logging,Internet Explorer 8,Console,据了解,它在测试版中,但不在发行版中?它在IE8中工作。点击F12打开IE8的开发者工具 >>console.log('test') LOG: test console.log仅在您打开开发人员工具后可用(F12可将其切换为打开和关闭)。 有趣的是,在您打开它之后,您可以关闭它,然后仍然通过console.log调用发布到它,这些调用将在您重新打开它时看到。 我认为这是一种缺陷,可能会被修复,但我们将拭目以待 我可能会用这样的方法: function trace(s) { if

据了解,它在测试版中,但不在发行版中?

它在IE8中工作。点击F12打开IE8的开发者工具

>>console.log('test')
LOG: test

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)
}
更简单的是:

function trace(s) {
  try { console.log(s) } catch (e) { alert(s) }
}

值得注意的是,IE8中的
console.log
不是真正的Javascript函数。它不支持
apply
call
方法。

如果您的所有console.log调用都“未定义”,这可能意味着您仍然加载了旧的firebuglite(firebug.js)。它将覆盖IE8的console.log的所有有效函数,即使它们确实存在。不管怎么说,这就是发生在我身上的事

检查覆盖console对象的其他代码

if (window.console && 'function' === typeof window.console.log) { window.console.log(o); } if(window.console&“function”==typeof window.console.log){ window.console.log(o); }
更好的回退方法是:


   var alertFallback = true;
   if (typeof console === "undefined" || typeof console.log === "undefined") {
     console = {};
     if (alertFallback) {
         console.log = function(msg) {
              alert(msg);
         };
     } else {
         console.log = function() {};
     }
   }

我真的很喜欢“orange80”发布的方法。这是优雅的,因为你可以设置一次,然后忘记它

其他的方法需要你做一些不同的事情(每次调用非普通的
console.log()
),这只是自找麻烦……我知道我最终会忘记的

我更进一步,将代码包装在一个实用函数中,您可以在javascript开始时调用一次,只要是在任何日志记录之前。(我正在我公司的事件数据路由器产品中安装此功能。它将有助于简化其新管理界面的跨浏览器设计。)

/**
*在开始时调用一次,以确保您的应用程序可以安全地调用console.log()和
*console.dir(),即使在不支持它的浏览器上也是如此。你可能没有用
*登录这些浏览器,但至少不会产生错误。
* 
*@param alertFallback-如果为“true”,则所有日志都将成为警报(如有必要)。
*(通常不适合生产)
*/
功能修复控制台(警报回退)
{    
如果(控制台类型==“未定义”)
{
console={};//如果它不存在,请定义它
}
if(typeof console.log==“未定义”)
{
if(alertFallback){console.log=function(msg){alert(msg);};}
else{console.log=function(){};}
}
if(typeof console.dir==“未定义”)
{
如果(警报回退)
{ 
//这是可以改进的…可能列出所有对象属性?
console.dir=函数(obj){alert(“dir:+obj”);};
}
else{console.dir=function(){};}
}
}

假设您不关心警报的回退,这里有一个更简洁的方法来解决Internet Explorer的缺点:

var console=console||{"log":function(){}};

这是我对各种答案的看法。我想实际查看记录的消息,即使在它们被触发时没有打开IE控制台,所以我将它们推入我创建的
控制台.messages
数组中。我还添加了一个函数
console.dump()
,以方便查看整个日志
console.clear()
将清空消息队列

此解决方案还“处理”其他控制台方法(我相信这些方法都源自)

最后,该解决方案是一种形式,因此不会污染全球范围。回退函数参数在代码底部定义

我只是把它放在我的主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)
一些额外的信息 行
var args=Array.prototype.slice.call(参数)
参数
对象创建数组。这是必需的,因为

trap()
是任何API函数的默认处理程序。我将参数传递给
消息
,这样您就可以获得传递给任何API调用的参数的日志(不仅仅是
console.log

编辑
我添加了一个额外的数组
console.raw
,它捕获传递给
trap()
的参数。我意识到,
args.join(“”)
正在将对象转换为字符串
“[object object]”
,这有时可能是不需要的。感谢您的支持。

对于缺少控制台的浏览器,最好的解决方案是:

// 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;
        }
    }
}());
我是在以下网站上找到的:

这是我的“请不要撞车”


我从上面使用了沃尔特的方法(见:)

我在这里找到了一个解决方案来正确显示对象

这意味着陷阱功能变为:

function trap(){
    if(debugging){
        // 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 index;
        for (index = 0; index < args.length; ++index) {
            //fix for objects
            if(typeof args[index] === 'object'){ 
                args[index] = JSON.stringify(args[index],null,'\t').replace(/\n/g,'<br>').replace(/\t/g,'&nbsp;&nbsp;&nbsp;');
            }
        }
        var message = args.join(' ');
        console.messages.push(message);
        // instead of a fallback function we use the next few lines to output logs
        // at the bottom of the page with jQuery
        if($){
            if($('#_console_log').length == 0) $('body').append($('<div />').attr('id', '_console_log'));
            $('#_console_log').append(message).append($('<br />'));
        }
    }
} 
函数陷阱(){
如果(调试){
//从arguments对象创建一个数组
var args=Array.prototype.slice.call(参数);
//console.raw捕获原始参数,而不转换为字符串
控制台。原始。推送(args);
var指数;
对于(索引=0;索引')。替换(/\t/g');
}
}
var message=args.join(“”);
console.messages.push(消息);
//我们不使用回退函数,而是使用下面几行输出日志
//在使用jQuery的页面底部
如果(美元){
if($('####console_log').length==0)$('body').append($('.attr('id','u console_log'));
$('.#_控制台_日志')。追加(消息)。追加($('
'); } } }

我希望这是有帮助的:-)

有这么多答案。我的解决方案是:

globalNamespace.globalArray = new Array();
if (typeof console === "undefined" || typeof console.log === "undefined") {
    console = {};
    console.log = function(message) {globalNamespace.globalArray.push(message)};   
}
简而言之,如果console.log不存在(或者在本例中未打开),则将日志存储在全局命名空间数组中。这样,你就不会被磨坊纠缠了
function trap(){
    if(debugging){
        // 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 index;
        for (index = 0; index < args.length; ++index) {
            //fix for objects
            if(typeof args[index] === 'object'){ 
                args[index] = JSON.stringify(args[index],null,'\t').replace(/\n/g,'<br>').replace(/\t/g,'&nbsp;&nbsp;&nbsp;');
            }
        }
        var message = args.join(' ');
        console.messages.push(message);
        // instead of a fallback function we use the next few lines to output logs
        // at the bottom of the page with jQuery
        if($){
            if($('#_console_log').length == 0) $('body').append($('<div />').attr('id', '_console_log'));
            $('#_console_log').append(message).append($('<br />'));
        }
    }
} 
globalNamespace.globalArray = new Array();
if (typeof console === "undefined" || typeof console.log === "undefined") {
    console = {};
    console.log = function(message) {globalNamespace.globalArray.push(message)};   
}
//one last double check against stray console.logs
$(document).ready(function (){
    try {
        console.log('testing for console in itcutils');
    } catch (e) {
        window.console = new (function (){ this.log = function (val) {
            //do nothing
        }})();
    }
});
(function(window) {

   var console = {};
   console.log = function() {
      if (window.console && (typeof window.console.log === 'function' || typeof window.console.log === 'object')) {
         window.console.log.apply(window, arguments);
      }
   }

   // Rest of your application here

})(window)
if (typeof console == "undefined" || typeof console.log === "undefined") {
    var oDiv=document.createElement("div");
    var attr = document.createAttribute('id'); attr.value = 'html-console';
    oDiv.setAttributeNode(attr);


    var style= document.createAttribute('style');
    style.value = "overflow: auto; color: red; position: fixed; bottom:0; background-color: black; height: 200px; width: 100%; filter: alpha(opacity=80);";
    oDiv.setAttributeNode(style);

    var t = document.createElement("h3");
    var tcontent = document.createTextNode('console');
    t.appendChild(tcontent);
    oDiv.appendChild(t);

    document.body.appendChild(oDiv);
    var htmlConsole = document.getElementById('html-console');
    window.console = {
        log: function(message) {
            var p = document.createElement("p");
            var content = document.createTextNode(message.toString());
            p.appendChild(content);
            htmlConsole.appendChild(p);
        }
    };
}