将所有Javascript控制台输出发送到DOM元素 [英] Sending all Javascript console output into a DOM element

查看:269
本文介绍了将所有Javascript控制台输出发送到DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将所有控制台输出发送到DOM元素,以便可以查看,而无需打开任何开发工具?我想查看所有输出,例如JS错误, console.log()输出等。

How does one send all console output into a DOM element so it can be viewed without having to open any developer tools? I'd like to see all output, such as JS errors, console.log() output, etc.

推荐答案

我发现接受的答案上面有帮助,但它有一些问题,如注释中所示:

I found the accepted answer above helpful but it does have a couple issues as indicated in the comments:

t在Chrome中工作,因为前不考虑这个上下文不再是控制台,修复是使用JavaScript应用方法。

1) doesn't work in Chrome because "former" does not take into account the this context no long being the console, the fix is to use the JavaScript apply method.

2)不会将多个参数传递到console.log

2) It does not account for multiple arguments being passed to console.log

我也希望这个可以在没有jQuery的情况下工作。

I also wanted this to work without jQuery.

    var baseLogFunction = console.log;
    console.log = function(){
        baseLogFunction.apply(console, arguments);

        var args = Array.prototype.slice.call(arguments);
        for(var i=0;i<args.length;i++){
            var node = createLogNode(args[i]);
            document.querySelector("#mylog").appendChild(node);
        }

    }

    function createLogNode(message){
        var node = document.createElement("div");
        var textNode = document.createTextNode(message);
        node.appendChild(textNode);
        return node;
    }

    window.onerror = function(message, url, linenumber) {
        console.log("JavaScript error: " + message + " on line " +
            linenumber + " for " + url);
    }

以下是
http://jsfiddle.net/eca7gcLz/

这篇关于将所有Javascript控制台输出发送到DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆