Sometimes when you come to browse the web, you will find that there is always a yellow error sign in the lower left corner of the IE browser. Sometimes the error pops up directly and cannot continue to browse the page. This is not conducive to the formality and authoritative development of the website.
Because of this error, the foreign banking page has caused a large number of users to lose money and dare not use the online banking of this bank, which has caused heavy losses.
Therefore, a mistake that cannot be made in terms of user experience and company strength is not to affect the use and cannot make users disgusted. I have always been committed to improving better code for everyone, so here we will organize some commonly used codes and how to use them. I hope that everyone can support us more and more in the future and let us develop together.
The first one: what I'm using
<SCRIPT language=javascript> <!-- =function(){return true;} // --> </SCRIPT>
How to use: Just add the above code to the head area of the web page you made the error.
The second type: It is targeted sometimes because some script errors cause the page to be unable to continue browsing. This problem is very serious, with a large number of users lost, or the inability to view your website at all.
This is not a simple script error problem. The main reason is that the code author has not considered and improved it. It must be modified carefully. However, if you really don’t know how to do it, then use such code.
try...catch can test errors in the code. The try part contains the code that needs to be run, and the catch part contains the code that is run when an error occurs.
grammar:
Copy the code. The code is as follows:
try { // Run the code here} catch(err) { //Error handling here}
Note: try...catch uses lowercase letters. Capital letters will go wrong.
Example 1
The following example was originally used to display the message "Welcome guest!" when the user clicked a button. However, alert() in the message() function is mistakenly written as adddlert(). The error occurred at this time:
<html> <head> <script type="text/javascript"> function message() { adddlert("Welcome guest!") } </script> </head> <body> <input type="button" value="View message" onclick="message()" /> </body> </html>
We can add the try...catch statement so that more appropriate actions can be taken when an error occurs.
The following example re-modifies the script using the try...catch statement. The error occurred because alert() was written by mistake. But this time, the catch part caught the error and handled it with a piece of prepared code. This code will display a custom error message to inform the user about what is going on.
<html> <head> <script type="text/javascript"> var txt="" function message() { try { adddlert("Welcome guest!") } catch(err) { txt="There is an error in this page.\n\n" txt+="Error description: " + + "\n\n" txt+="Click OK to continue.\n\n" alert(txt) } } </script> </head> <body> <input type="button" value="View message" onclick="message()" /> </body> </html>
Example 2
The next example will display a confirmation box, allowing the user to choose whether to click the OK button to continue browsing the web page when an error occurs, or to click the Cancel button to return to the homepage. If the return value of the confirm method is false, the code will redirect the user to another page. If the return value of the confirm method is true, the code will do nothing.
<html> <head> <script type="text/javascript"> var txt="" function message() { try { adddlert("Welcome guest!") } catch(err) { txt="There was an error on this page.\n\n" txt+="Click OK to continue viewing this page,\n" txt+="or Cancel to return to the home page.\n\n" if(!confirm(txt)) { ="http:///" } } } </script> </head> <body> <input type="button" value="View message" onclick="message()" /> </body> </html>
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!