Question

Is there a way to fire an event on every console.log() which will include either the parameters passed in the log function or the data returned by it? What I want to do is track every console.log execution and pass the information in a div of the page.

I don't mind whether it's a JavaScript or a JQuery answer since I'm using both in this project.

Était-ce utile?

La solution

<script language="javascript">
    // cache a reference to the log method on the console object
    var _consoleLog = console.log;

    // replace the existing log method on the console object
    console.log = function() {
            // your custom action
        alert(arguments[0]);

            // pass control to the cached log method
        return _consoleLog.apply(console, arguments);
    };

    // quick test
    console.log("hello");
</script>

This will do the trick

Autres conseils

Override jQuery functions

Try something like this:

var doSomethingElse = function(data) {
  $("#output").html(data);
};
var f = function() { 
  console.log(this); 
  doSomethingElse(this);
};
f.apply("Hello World", null); 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top