问题
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?
回答1:
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
回答2:
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?
来源:https://stackoverflow.com/questions/35759269/redirect-javascript-syntax-errors-and-console-log-to-somewhere-else