A solution that can be used to debug JavaScript errors

Source: Internet
Author: User

A uses the alert () and document. Write () methods to monitor variable values.
If you want to interrupt Code To monitor the value of the variable, use the alert () method;
If you want to view a large number of values, use the document. Write () method to avoid repeated clicking the "OK" button;

B. Use the window. onerror event
When an exception occurs on the page, the onerror event is triggered on the window object. It can tell developers related error information to a certain extent.
Example:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
Function myerror (_ message, _ URL, _ line)
{
Alert ("error message:" + _ message
+ "\ N error URI:" + _ URL
+ "\ N error row:" + _ line
);
Return true; // Block System Events
}
// Bind an error event
Window. onerror = myerror;
// Trigger error example:
Window, onload = test;
</SCRIPT>

Note: in IE, after an error event is triggered, the normal code continues to run. All variables and data are saved and can be accessed normally in the onerror event handling method; in Firefox, after an error event is triggered, all the variables and data are destroyed.

C. Use try... catch statements to find errors
Example:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
Try
{
Alert (trigger exception );
}
Catch (_ ex) // You can omit the "_ ex" parameter.
{
VaR err = "error message ";
For (var I in _ ex)
{
Err + = "\ n parameter name:" + I
+ "\ T parameter value:" + _ ex [I];
}
Alert (ERR); // print error
}
Finally // finally can be omitted...
{
Alert ("Finally always runs ");
}
</SCRIPT>

Note: Try... catch does not properly handle JavaScript syntax errors.
Example:Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
Try
{
Alert ("trigger syntax error"); // if there is more than half side ")"
}
Catch (_ ex) // The _ ex parameter can be omitted
{
VaR err = "error message ";
For (var I in _ ex)
{
Err + = "\ n parameter name:" + I
+ "\ T parameter value:" + _ ex [I];
}
Alert (ERR); // print error
}
</SCRIPT>

This example does not enter the Catch Block.

D. Use the relevant Debugger
In IE and Firefox browsers, you can use the relevant debugger or plug-in to debug JavaScript.

● In Firefox, you can use the built-in "error console ". The procedure is as follows:
Open the Firefox browser → In the menu bar "tools" → select "error console.
Without other plug-ins, its built-in "error console" is a very good choice.

In addition, there are some good debuggers In the Firefox browser, such as Venkman and firebug.
After the Venkman debugger is installed, you can go to the Firefox browser → In the menu bar "tools" → select the "JavaScript debugger" command to enable it;
After the firebug debugger is installed, you can choose Firefox> Tools> firebug> open firebug;

● In the IE browser, you can use the Microsoft Script debugger
Microsoft Script debugger is an IE Plug-In released by Microsoft with IE 4, it can be downloaded from Microsoft's official website for free.
after the download and installation, you must enable the debugging option of the IE browser. The procedure is as follows:
1> open the IE browser → select "Tools"> "Internet Options"> "advanced" tab on the menu bar → disable script debugging (Internet Explorer) "check box to remove the check box.
2> when the IE browser is browsing the page, run the Microsoft Script debugger tool for debugging.
on the running document Panel of the Microsoft Script debugger, select the enabled page file (read-only) and press F9 to set breakpoint debugging. In addition, its command window panel is also a useful function. When the code breakpoint is stopped, you can enter the variable name and press enter to view the variable value; the command window panel can even accept simple Javascript commands. However, the Microsoft Script debugger has a bug.

Related Article

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.