JS interception/capture global Error onerror

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.