Home >Web Front-end >JS Tutorial >javascript fault-tolerant processing code (shielding js errors)_javascript skills

javascript fault-tolerant processing code (shielding js errors)_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:29:301248browse

It is because of such errors on foreign bank pages that a large number of users are lost and they are afraid to use the bank's online banking, resulting in heavy losses.

So a mistake that is not allowed to occur in terms of user experience and company strength, even if it does not affect the use and does not offend users, Script House has always been committed to improving better code for everyone, so here We will sort out some commonly used codes and how to use them. We hope that everyone will support us more and more in the future and let us develop together.

The first type: Script House also uses it

Copy code The code is as follows:



How to use: Just add the above code to the head area of ​​your error webpage.

The second type: It is aimed at the page that sometimes cannot be browsed due to some script errors. This problem is very serious, resulting in a large loss of users, or the inability to view you at all.
This is not a simple script error problem. The main reason is that the author of the code has not considered perfection. It must be modified carefully. But if you really don’t know how, then use such code
try...catch can test for errors in your code. The try section contains the code that needs to be run, while the catch section contains the code that runs when an error occurs.
Syntax:
Copy code The code is as follows:

try
{
//Run code here
}
catch(err)
{
//Handle errors here
}

Note: try... catch uses lowercase letters. Capital letters will go wrong.
Example 1
The following example was originally used to display the "Welcome guest!" message when the user clicks the button. However, alert() in the message() function is mistakenly written as adddlert(). At this time the error occurred:

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

We can add try.. .catch statement so that more appropriate action can be taken when an error occurs.
The following example re-modifies the script using try...catch statements. The error occurred because alert() was written by mistake. But this time, the catch section catches the error and handles it with a prepared piece of code. This code will display a custom error message to inform the user of what happened.

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

Example 2
Next An example would display a confirmation box that allows the user to choose whether to click the OK button to continue browsing the page or click the Cancel button to return to the home page when an error occurs. If the return value of the confirm method is false, the code will redirect the user to another page. If the confirm method returns true, the code does nothing.
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

More related For js fault tolerance processing, you can refer to the following two articles:

JavaScript Try...Catch statementJavaScript fault tolerance exception handling
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:js uses AND or operator priority to implement if else conditional judgment expression_javascript skillsNext article:js uses AND or operator priority to implement if else conditional judgment expression_javascript skills

Related articles

See more