Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

javascript write console log to pop up

I am trying to extract the content of the console and have it sent as a pop up using javascript. The reason for this is I have lots of information being written to the console but I am unable to read the console in a particular environment. I have attempted to follow the answer to this question but have gotten errors: Can I extend the console object (for rerouting the logging) in javascript? Here is my code followed by the errors I have gotten:

<p id="output"></p>

<script>
    console.log("asdf")

    (function() {
        var exLog = console.log;
        console.log = function(msg) {
            exLog.apply(this, arguments);
            alert(msg);
        }
    })()

</script>

this is the error

Uncaught TypeError: console.log(...) is not a function

Thanks for the help

Thanks for getting rid of the errors, as I stated in the question my goal is to be able to read errors that occur while the program runs. here is an example of what I want:

<script>


    function func(){
        return y
    }

    (function() {
        var exLog = console.log;
        console.log = function(msg) {
            exLog.apply(this, arguments);
            alert(msg); 
        }
    })()


    func()


</script>

I want this to have the error that occurs to pop up
(the error is Uncaught ReferenceError: y is not defined)

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can reproduce your error.

But if I call console.log after defining it, it works as you expect

<script>


    (function() {
        var exLog = console.log;
        console.log = function(msg) {
            exLog.apply(this, arguments);
            alert(msg);
        }
    })()

    console.log("asdf"); // calling after defining it

</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...