Javascript FAQ (end 25)-error handling

Source: Internet
Author: User

22. Error Handling

 

1. Disable JS errors (Suppress JS errors)

Q:Can I disable javascript error messages?

A:Yes. To disable all JavaScript error messages on the page, you need to put the following code in the head part of the page:

<SCRIPT language="JavaScript"><!--function silentErrorHandler() {return true;}window.onerror=silentErrorHandler;//--></SCRIPT>

Or you can use similar code in the. js file introduced by the page (without the script tag ). For code examples that can run, refer to our error handling demo program.

 

2. add your own error handler (adding your own error handler)

Q:Can I set my own error handling program?

A:Of course. Use the following JavaScript code to define your own error handler:

function handlerFunction(description,page,line) { // put error-handling operators here return true}window.onerror=handlerFunction

The following optional parameters can be used for your error handling function:

  • Incorrect text description
  • Error page address
  • Error row number

If you want this error handler to call the default error handling function of the browser after processing the custom error, false must be returned. If you do not want to call the default error handling mechanism of the browser, the function must return true. For the sample code, see the error handling demo program.

 

3. Error Handling demo)

Q:Can I dynamically change the Javascript error handler?

A:Yes. To change the Javascript error handler, you only need to set the window. onerror value to the function name, which will serve as your new error handler.

Here is a demo program. You can test three different error handling programs:

  • Default browser error handling method
  • Error handler that displays the custom warning information box
  • Disable "quiet" error handlers for all error messages
  1. Use the selection box to set or change the error handling program.
  2. Click "fire an error" to test the error handling program of the activity.

The following is the source code of the error handler in the demo program:

function defaultHandler() {return false}function silentHandler()  {return true}function customHandler(desc,page,line,chr)  { alert(  'JavaScript error occurred! /n' +'The error was handled by ' +'a customized error handler./n' +'/nError description: /t'+desc +'/nPage address:      /t'+page +'/nLine number:       /t'+line ) return true}

Conclusion:

This is the last article in The javascript FAQ series. Through this series of translations, I have solved many unclear problems in the past, and hope to bring some gains to those who have seen this series of translations. Most of the discussions in this series of articles are based on the old version of browsers. There may be some new solutions for some problems. I hope you can actively discuss them.

Due to my limited level, although I have considered many sentences in translation, it is inevitable that I cannot express the meaning of the original text. I hope you can correct me. If you prefer e, you can click the link on each topic to view the original text.

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.