Home > Web Front-end > H5 Tutorial > body text

Method to capture console.log() output in JS

高洛峰
Release: 2017-02-03 14:46:06
Original
3682 people have browsed it

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template