Solutions that you can use to debug JavaScript errors _javascript Tips

Source: Internet
Author: User
A Use the alert () and document.write () methods to monitor variable values
If you want to break the code and monitor the value of the variable, use the alert () method;
If you need to see a lot of values, use the document.write () method to avoid repeatedly clicking the OK button;

B using the Window.onerror event
When an exception occurs on the page, the OnError event is triggered on the Window object. It can tell developers a certain amount of error information.
Example:
Copy Code code as follows:

<script type= "Text/javascript" >
function Myerror (_message,_url,_line)
{
Alert ("error message:" + _message
+ "\ n wrong uri:" + _url
+ "\ n Wrong number of rows:" + _line
);
return true; Events for shielding Systems
}
Binding error Events
Window.onerror = Myerror;
Example of triggering error:
Window,onload = test;
</script>

Note: In IE, after the error event is triggered, the normal code will continue to run, all the variables and data will be saved, in its OnError event processing method can be normal access to, and in Firefox, triggered the error event, all the end, all the variables and data will be destroyed.

C use Try...catch statement to find errors
Example:
Copy Code code as follows:

<script type= "Text/javascript" >
Try
{
Alert (triggering 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 handle JavaScript syntax errors very well.
Example:
Copy Code code as follows:

<script type= "Text/javascript" >
Try
{
Alert ("Trigger syntax error")); More than half a ")"
}
catch (_EX)//can omit _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
}
</script>

The example does not enter a catch block.

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

In the Firefox browser, you can use its own error console. Action steps are as follows:
Open the Firefox browser → in the menu bar "Tools" → select "Error Console".
In the absence of other plug-ins, its own "error console" is a very good choice.

In addition, in the Firefox browser, there are some very good debugger, such as: Venkman, Firebug and so on.
Venkman debugger installed, you can in the Firefox browser → in the menu bar "Tools" → Select the "JavaScript Debugger" command to enable;
Firebug debugger installed, you can in the Firefox browser → in the menu bar "tool" → select "Firebug" → select "Open Firebug" can be;

In IE browser, you can use the Microsoft Script Debugger Debugger
Microsoft Script Debugger is an IE plugin that Microsoft has released with IE 41 and can be downloaded free of charge from Microsoft's official website.
After downloading the installation, you must open the debug options for IE browser to use. Action steps are as follows:
1> open IE browser → Select the menu bar "Tools" → "Internet options" command → "advanced" tab → remove the tick in the Disable scripting Debugging (Internet Explorer) check box.
2> when IE browser is browsing the page, run the Microsoft Script Debugger Debugger tool to debug.
Select the open paging file (read-only) in the Running document panel of the Microsoft Script Debugger Debugger, and then press F9 to set breakpoint debugging. In addition, its Command Window panel is also a useful feature that allows you to see the value of a variable when a code breakpoint is stopped and enter a variable name in it, and the Command Window panel can even accept simple JavaScript commands. However, there is a bug problem with the Microsoft Script Debugger debugger itself.
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.