Mathew
Mathew

Reputation: 1440

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

Upvotes: 0

Views: 2329

Answers (2)

Tallboy
Tallboy

Reputation: 13417

Your code works as is, just change where you call console.log() (put it after)

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

console.log("hello")

Upvotes: 0

Adelin
Adelin

Reputation: 8209

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>

Upvotes: 1

Related Questions