3

I'm trying to send whatever would normally appear in the javascript console to my own custom functions.

I've tried the following

window.console.error = window.console.debug =  window.console.log
window.console.log=mylog;

function mylog(msg){
    //send it somewhere just using alert as an example
      alert("log="+msg);    
}
console.log("yo");
var x=y;//a syntax error

Using the code above I only see the following alert "yo"

In the console log I see "x is not defined"
How do I redirect the syntax errors?

user873477
  • 87
  • 2
  • 8

2 Answers2

0

You can use onerror it is designed to catch syntax errors.

window.onerror = function(message, source, lineno, colno, error) {
   alert(message);
}

Offical docs on MDN

Victory
  • 5,811
  • 2
  • 26
  • 45
0

Here's a thread on this. Note the answer that shows how to "hijack" console.log() and send it wherever you want: Capturing javascript console.log?

Community
  • 1
  • 1
Bill
  • 31
  • 4