JS interception/capture global Error onerror
During mobile Web development, some problems that cannot be reproduced during real-machine testing may often occur during PC debugging. At this time, we need to intercept errors on the mobile phone and provide relevant output. The company and the Internet have similar tools/class libraries. However, if a pure simple debugging does not require the introduction of tools or class libraries, we only need to know the principle of global interception. The window. onerror syntax is actually very simple:
onerror=handleErrfunction handleErr(msg,url,l){//Handle the error herereturn true or false}
Whether the browser displays standard error messages depends on the onerror return value. If the returned value is false, an error message is displayed on the console (JavaScript console. Otherwise, no. Example: The following example shows how to use the onerror event to capture errors:
In addition, if we want to directly simulate this process on the chrome console, we will find that using throw new Error directly cannot trigger this onerror. This may be because the Console environment is different from the page environment. But in another way, it can be triggered: setTimeout (function () {throw new Error}, 1000): p is really witty.