Sometimes when you browse the web, you find that there is always a yellow error mark in the lower left corner of the IE browser. Sometimes an error pops up directly and you cannot continue to browse the page. This is not good for the formal and authoritative development of the website.
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 itself also uses
The code is as follows:
<SCRIPT language=javascript> <!-- window.onerror=function(){return true;} // --> </SCRIPT>
How to use : Just add the above code to the head area of your error web page.
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:
The code is as follows:
try { //在此运行代码 } catch(err) { //在此处理错误 }
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 an error occurred:
<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 a try...catch statement so that more appropriate measures 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.
<html> <head> <script type="text/javascript"> var txt="" function message() { try { adddlert("Welcome guest!") } catch(err) { txt="此页面存在一个错误。\n\n" txt+="错误描述: " + err.description + "\n\n" txt+="点击OK继续。\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 to click the OK button to continue browsing the web page when an error occurs, or 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 confirm method returns true, the code does 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)) { document.location.href="http://www.w3school.com.cn/" } } } </script> </head> <body> <input type="button" value="View message" onclick="message()" /> </body> </html>