JavaScript fault-tolerant processing code (masking JS error) _javascript tips

Source: Internet
Author: User
Foreign Bank page is because of such a mistake, causing a large number of users to lose the bank's online banking and other causes of heavy losses.

So a mistake that you want to be in the user experience and the strength of the company, is not to affect the use of users can not be disgusted with, cloud-Habitat community has been committed to improve the code for everyone, so here we will organize some common code, the use of what, I hope you will be more and more support for us, Let us develop together.

the first: the cloud-dwelling community itself is using
Copy Code code as follows:

<script language=javascript>
<!--
Window.onerror=function () {return true;}
-->
</SCRIPT>

How to use: Add the above code to the head area of your error page.

Second Kind: The problem is that sometimes the page can't continue browsing because of some scripting errors, which is very serious, a lot of user churn, or it's impossible to view your site on the day
This is not a simple script error problem, mainly the code author does not consider the perfect, must be well modified, but if you really do not, then use this code
Try...catch can test errors in your code. The try section contains the code that needs to be run, and the catch part contains the code that runs when the error occurs.
Grammar:
Copy Code code as follows:

Try
{
Run code here
}
catch (Err)
{
Handling Errors Here
}

Note: Try...catch uses lowercase letters. Uppercase letters can go wrong.
Instance 1
The following example was originally used to display the message "Welcome guest!" when the user clicked on the button. However, alert () in the message () function is mistakenly written as Adddlert (). Then the error occurred:
<ptml> <pead> <script type= "Text/javascript" > Function message () {Adddlert ("Welcome guest!") } </script> </pead> <body> <input type= "button" value= "View message" onclick= "message ()"/> & Lt;/body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

We can add Try...catch statements so that we can take more appropriate action when the error occurs.
The following example modifies the script with the Try...catch statement. The error occurred because of the mistake of writing alert (). This time, however, the catch section catches the error and uses a prepared code to handle the error. This code displays a custom error message to tell the user what happened.
<ptml> <pead> <script type= "Text/javascript" > var txt= "" function message () {try {Adddlert ("We Lcome guest! ")} catch (Err) {txt=) there is an error in this page. \ n \ nthe "txt+=" error Description: "+ Err.Description +" \ n "txt+=" click OK to continue. \ n \ nthe "alert (TXT)}} </script> </pead> <body> <input type=" button "value=" View message "Onclic k= "message ()"/> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Instance 2
The next example displays a confirmation box that lets the user choose to continue browsing the page by clicking the OK button when the error occurs, or by clicking the Cancel button to return to the homepage. If the return value of the Confirm method is false, the code redirects the user to another page. If the return value of the Confirm method is true, then the code does nothing.
<ptml> <pead> <script type= "Text/javascript" > var txt= "" function message () {try {Adddlert ("We Lcome guest! ")} catch (Err) {txt= "There was a" error on this page.\n\n "txt+=" click OK to continue viewing this page,\n "txt+=" or Canc El to return to the home page.\n\n ' if (!confirm (TXT)) {document.location.href= ' http://www.w3school.com.cn/'}}} </script> </pead> <body> <input type= "button" value= "View message" onclick= "message ()"/> < /body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Related to more JS fault-tolerant processing can refer to the following two articles:
JavaScript Try ... Catch statement

JavaScript fault-tolerant exception handling
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.