22. Error handling
1, Prohibit JS error (suppress JS Errors)
Q: Can i disable JavaScript error messages?
A: That 's OK. To disable all JavaScript error messages on the page, you should put the following code in the Head section of the page:
<script language= "JavaScript" >
<!--
function Silenterrorhandler () {return true;}
Window.onerror=silenterrorhandler;
-->
</SCRIPT>
Or how you have the. js file introduced into the page, where you can use similar code (without the script tag). Refer to our error handling demo for a code instance that can be run.
2. Add your own error handler (adding Your Own error Handler)
Q: can I set my own error handler?
A: of course you can. Use the following JavaScript code to define your own error handlers:
function Handlerfunction (description,page,line) {
//Put error-handling operators here return
true
}
Window.onerror=handlerfunction
Your error handler can use the following optional parameters: Incorrect text describing the number of page address errors that occurred with the error
You must return FALSE if you want this error handler to call the browser's default error handling after you complete the custom error handling. If you do not want to invoke the default error handling mechanism of the browser, the function must return true. The code example references an error-handling demo.
3. Error handling Demo program (handling Demo)
Q: can I dynamically change JavaScript's error handlers?
A: That 's OK. To change the JavaScript error handler, you only need to set the Window.onerror value to the name of the function, and it will be your new error handler.
Here is a demo program where you can test three different error handlers: Browser default error handling method displays custom warning message box error handlers prohibit "quiet" error handlers for all incorrect information
Custom Error handlersilent Error Handlerdefault error Handler Use the selection box to set or change the fault handler. Click "Fire an Error" To test the error handler for the activity.
The following is the source code for 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
' + ' + ') ' is ' handled by ' + ' a Customized Error handler./n '
+ '/nerror Description:/t ' +desc
+ '/npage address:/t ' +page
+ '/nline Number:/t ' +line
) return
true
}
written at the end:
This article is the last of the JavaScript FAQ series. Through this series of translation so that I have a lot of ambiguous problems have been resolved, and I hope to see this series of translators to bring some gains. Because the discussion of this series of articles is mostly based on the old version of the browser, there may be some problems with a new solution, I hope you can actively discuss.
Because of my limited level, although the translation of a lot of sentences to consider repeatedly, but inevitably there can not express the meaning of the original place, I hope you correct me. And if you are good e, you can directly click on each topic link, directly to see the original text.