javascript Fault Tolerant Handling Code of Masks js Errors

  • 2021-07-13 03:45:36
  • OfStack

Sometimes when you browse the web page, you find that there is always a yellow error sign in the lower left corner of IE browser, and sometimes you can't continue browsing the page directly, which is unfavorable to the formal and authoritative development of the website.

Foreign bank page is because of such mistakes, resulting in a large number of users are afraid to use this bank's online banking, resulting in heavy losses.

Type 1: This site is in use


<SCRIPT language=javascript> 
<!-- 
window.onerror=function(){return true;} 
// --> 
</SCRIPT> 

Usage: Add the above code to the head area of your error page.

Type 2: It is aimed at sometimes because of 1 script error, the page can't continue to browse, this problem is very serious, a large number of users are lost, or you can't view your website at all

This is not a simple script error problem, mainly because the code author did not consider perfect, 1 must be well modified, but if you really can't, 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 the error occurs.

Syntax:

Copy code


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

Note: try... catch uses lowercase letters. Capital letters will make mistakes.

Example 1

The following example was originally used to display "Welcome guest! "The news. However, alert () in the message () function is mistakenly written as adddlert (). Then the 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 the try... catch statement so that we can take more appropriate action when an error occurs.

The following example re-modifies the script with the try... catch statement. An error occurred because alert () was written by mistake. This time, however, the catch section caught the error and handled it with a piece of prepared 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=" This page exists 1 A mistake. \n\n" 
txt+=" Error description : " + err.description + "\n\n" 
txt+=" Click OK Go on. \n\n" 
alert(txt) 
} 
} 
</script> 
</head> 
<body> 
<input type="button" value="View message" onclick="message()" /> 
</body> 
</html> 

Example 2

The next example displays a confirmation box, allowing the user to choose whether to click OK to continue browsing the web when an error occurs, or to click Cancel to return to the home page. 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 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>

Related articles: