Quick locating of JS errors-JavaScript debugging skills

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.