Javascript error tolerance code (blocking js errors)

Source: Internet
Author: User

The foreign bank page is caused by this error, resulting in the loss of a large number of users and the failure to use the bank's online banking and other causes heavy losses.

Therefore, if you want to avoid errors in terms of user experience and company strength, that is, they will not affect your use of the service or dislike the service. You have always been committed to improving your code, so here we will organize some common code and how to use it. I hope you will be able to support us more and more in the future and let us develop together.

First, the home of the feet is also in use.
Copy codeThe Code is as follows:
<SCRIPT language = javascript>
<! --
Window. onerror = function () {return true ;}
// -->
</SCRIPT>

Usage: add the above Code to the head area of the webpage with an error.

Second: Sometimes the page cannot be browsed due to some script errors. This problem is very serious. A large number of users are lost, or you cannot view the day of your website at all.
This is not a simple script error. It is mainly because the code author has not considered the improvement and must make proper modifications. However, if you do not, use this code.
Try... catch can be used to test errors in the code. The try part contains the code to be run, and the catch part contains the code that runs when an error occurs.
Syntax:
Copy codeThe Code is as follows:
Try
{
// Run the code here
}
Catch (err)
{
// Handle errors here
}

Note: try... catch uses lower-case letters. An error occurs when uppercase letters are used.
Instance 1
The following example shows "Welcome guest!" when you click the button! "This message. However, alert () in the message () function is mistakenly written as adddlert (). The following error occurs:
<Html> <pead> </pead> <body> <input type = "button" value = "View message" onclick = "message () "/> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
We can add try... catch statements so that more appropriate measures can be taken when an error occurs.
The following example uses the try... catch statement to modify the script again. The error occurred because alert () was mistakenly written. But this time, the catch part caught the error and used a piece of prepared code to handle the error. This code will display a custom error message to inform the user of what happened.
<Html> <pead> </pead> <body> <input type = "button" value = "View message" onclick = "message () "/> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Instance 2
In the next example, a confirmation box is displayed, asking the user to choose to click the OK button when an error occurs to continue browsing the webpage, or click 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, the code will not do anything.
<Html> <pead> </pead> <body> <input type = "button" value = "View message" onclick = "message () "/> </body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

For more js fault tolerance Processing, refer to the following two articles:
JavaScript Try... Catch statement

JavaScript Fault Tolerance 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.