Home  >  Article  >  Web Front-end  >  Method to capture console.log() output in JS_javascript skills

Method to capture console.log() output in JS_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:03:321255browse

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.

Statement:
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