JavaScript fault-tolerant processing code (masking JS errors)

Source: Internet
Author: User

Sometimes people to browse the Web page found that there is always a yellow error mark in the lower left corner of IE browser, and sometimes the direct pop-up error can not continue to browse the page, so that the formal and authoritative development of the site unfavorable.

The foreign bank page is because of such a mistake, resulting in a large number of users lost the fear of using the bank's online banking and so on caused heavy losses.

So a want in the user experience and the company's strength is not to appear in the error, is not to affect the use of users can not be offensive, the script home has been committed to improve the code for everyone, so here we will tidy up some common code, use the method what, I hope we can more and more support us, Let's develop together.

The first: the Scripting House is used by itself.

Copy CodeThe code is 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.

The second kind : for sometimes because some script error, cause the page cannot continue to browse, this problem is very serious, the user's massive loss, or simply cannot view your website inside day
This is not a simple script error problem, mainly because 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 for errors in the code. The try section contains the code that needs to be run, and the Catch section contains the code that runs when the error occurs.
Grammar:

Copy CodeThe code is as follows:
Try
{
Run the code here
}
catch (Err)
{
Handling Errors Here
}


Note: Try...catch uses lowercase letters. Uppercase letters make an error.
Example 1
The following example is used to display the message "Welcome guest!" when the user taps the button. However, alert () in the message () function is mistakenly written as Adddlert (). At this point the error occurred:

<textarea id="runcode54919"><ptml> <pead> <script type= "Text/javascript" > Function message () {Adddlert ("Welcome guest!")} &L t;/script> </pead> <body> <input type= "button" value= "View message" onclick= "message ()"/> </ Body> </ptml></textarea>[Ctrl + A full selection Note: If you need to introduce external JS need to refresh to execute]


We can add Try...catch statements so that more appropriate action can be taken when an error occurs.
The following example re-modifies the script with the Try...catch statement. The error occurred because the alert () was mistakenly written. This time, however, the catch section catches the error and uses a piece of prepared code to handle the error. This code displays a custom error message to tell the user what happened.

<textarea id="runcode46633"><ptml> <pead> <script type= "Text/javascript" > var txt= "" function message () {try {Adddlert ("Welco Me guest! ")} catch (Err) {txt= "There is an error on this page. \ n \ nthe "txt+=" error Description: "+ Err.Description +" \ n \ nthe "txt+=" click OK to continue. \ n \ nthe "alert (TXT)}} </script> </pead> <body> <input type=" button "value=" View message "onclick=" mes Sage () "/> </body> </ptml></textarea>[Ctrl + A full selection Note: If you need to introduce external JS need to refresh to execute]


Example 2
The next example displays a confirmation box that allows the user to choose whether to continue browsing the page by clicking the OK button when an 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.

<textarea id="runcode24606"><ptml> <pead> <script type= "Text/javascript" > var txt= "" function message () {try {Adddlert ("Welco Me guest! ")} catch (Err) {txt= "There is an error in this page.\n\n" txt+= "click OK to continue viewing this page,\n" txt+= "or Cancel t o 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></textarea>[Ctrl + A full selection Note: If you need to introduce external JS need to refresh to execute]

JavaScript fault-tolerant processing code (masking JS errors)

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.