0

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

8
  • 3
    How about calling console.log after defining it? Commented Jan 16, 2018 at 8:20
  • Im not sure what you mean Commented Jan 16, 2018 at 8:20
  • Thanks for getting it working so far, I want to be able to get the errors written to the pop up though Commented Jan 16, 2018 at 8:28
  • What is the environment in which you run your code? I tested it in Chrome and after redefining the console.log function as you did, I get the message logged and alerted. Commented Jan 16, 2018 at 8:30
  • I am currently running in chrome Commented Jan 16, 2018 at 8:31

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.