It is very common to use JavaScript in Webpage writing. However, JS program debugging is quite depressing. Javascript is a weak scripting language and many errors cannot be controlled. When Javascript is faulty, only a simple error prompt dialog box is displayed in IE browser. The errors reported by IE are often inexplicable.
Javascript error locating is a headache. How can I quickly locate JS errors? This article describes how to quickly locate JS errors.
Developers who have written JavaScript know that JS errors are difficult to locate. errors such as missing objects are always reported, and incorrect pages and lines are reported, which is difficult to locate. Here I will introduce a simple and effective method for JS errors:
The onerror event is a standard method for capturing JavaScript errors on a webpage. As long as a script error occurs on the page, an onerror event is generated.
To use the onerror event, you must create a function to handle the error ,.
Window. onerror = function (smessage, Surl, Sline ){
// Handle errors
Return true
}
Window. onerror has three parameters:
1> smessage is the error message.
2> Surl is the URL of the page with an error
3> Sline is the code line with an error. (if it is an error on the current page, the number of lines is accurate. If it is not an error on the current page, the number of lines is usually staggered up and down .)
The returned value of onerror determines whether the browser displays a standard error message. If false is returned, the browser displays the standard error information in the console of javascrui. If true is returned, the browser does not display standard error messages.
Of course, this information is not enough. In fact, we can get more prompts, such as the wrong function call stack.
The following is an example of code:
// Function for displaying error messages
Function reporterror (smessage, Surl, Sline ){
VaR STR = "";
STR + = "error message:" + smessage + "\ n ";
STR + = "error address:" + Surl + "\ n ";
STR + = "Number of error rows:" + Sline + "\ n ";
STR + = "<========= call stack ==========>\ N ";
VaR func = Window. onerror. Caller;
VaR Index = 0;
While (func! = NULL ){
STR + = "Number" + index + "function:" + func + "\ n ";
STR + = "Number" + index + "function: parameter table :"
For (VAR I = 0; I ????)
STR + = func. Arguments [I] + ",";
}
STR + = "\ n ===============================\ N ";
Func = func. Caller;
Index ++;
}
Alert (STR );
Return true;
}
// Whether the browser displays standard error messages depends on the onerror return value. If the returned value is false, an error message is displayed on the console (JavaScript console. Otherwise, no.
Window. onerror = reporterror;
In this way, the function call stack and the parameter values of each function are obtained to better locate JS errors.
Note: The onerror event must be earlier than other javascript programs on this page!
Function reporterror (smessage, Surl, Sline ){
VaR STR = "";
STR + = "error message:" + smessage + "\ n ";
STR + = "error address:" + Surl + "\ n ";
STR + = "Number of error rows:" + Sline + "\ n ";
STR + = "<========= call stack ==========>\ N ";
VaR func = Window. onerror. Caller;
VaR Index = 0;
While (func! = NULL ){
// STR + = "nth" + index + "function:" + func + "\ n ";
// STR + = "Number" + index + "function: parameter table :"
// For (VAR I = 0; I <func. Arguments. Count; I ++)
// STR + = func. Arguments [I] + ",";
//}
STR + = func;
STR + = "\ n ===============================\ N ";
Func = func. Caller;
Index ++;
}
Alert (STR );
Return true;
}
Window. onerror = reporterror;