Home > Article > Web Front-end > Method to capture console.log() output in JS
The example in this article describes the method of capturing console.log() output in JS. Share it with everyone for your reference. The specific analysis is as follows:
We know that console.log() can output information to the debugger for developers to view. But what if we want to get the output of console.log() in JS? In fact, it is not difficult. Just save the original console.log first and then replace it with another implementation. The code is as follows:
var lastLog; console.oldLog = console.log; console.log = function(str) { console.oldLog(str); lastLog = str; } console.log("Hello, Neo"); document.write(lastLog);
At this time, "Hello, Neo" is saved in lastLog.
I hope this article will be helpful to everyone’s JavaScript programming design.
For more articles related to methods of capturing console.log() output in JS, please pay attention to the PHP Chinese website!