Home >Web Front-end >JS Tutorial >Solutions you can use to debug JavaScript errors_javascript tips

Solutions you can use to debug JavaScript errors_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:21:381327browse

A Use the alert() and document.write() methods to monitor variable values ​​
If you want to interrupt the running of the code and monitor the value of the variable, use the alert() method;
If there are many values ​​that need to be viewed, use document .write() method to avoid repeatedly clicking the "OK" button;

B Use the window.onerror event
When an exception occurs on the page, the onerror event will be triggered on the window object. It can tell developers relevant error information to a certain extent.
Example:

Copy code The code is as follows:



Note: In IE, after the error event is triggered, the normal code will continue to run, and all variables and data will be saved and can be accessed normally in its onerror event handling method; in Firefox, after the error event is triggered, everything is over and all variables and data will be destroyed.

C Use try...catch statement to find errors
Example:
Copy code The code is as follows:



Note: try...catch does not handle JavaScript syntax well mistake.
Example:
Copy code The code is as follows:



This example does not enter the catch block.

D Use relevant debuggers
In IE and Firefox browsers, you can use relevant debuggers or plug-ins to debug JavaScript.

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

In addition, in the Firefox browser, there are some very good debuggers, such as: Venkman, Firebug, etc.
After the Venkman debugger is installed, it can be enabled in the Firefox browser → in the menu bar "Tools" → select the "JavaScript Debugger" command;
After the Firebug debugger is installed, it can be enabled in the Firefox browser → in the menu bar " "Tools" → Select "Firebug" → Select "Open Firebug";

● In IE browser, you can use the Microsoft Script Debugger debugger
Microsoft Script Debugger is released with IE 4 by Microsoft An IE plug-in can be downloaded for free from Microsoft's official website.
After downloading and installing, the debugging option of the IE browser must be turned on before it can be used. The steps are as follows:
1> Open the IE browser → Select "Tools" on the menu bar → "Internet Options" command → "Advanced" tab → Check the "Disable script debugging (Internet Explorer)" check box Just remove it.
2> When the IE browser is browsing the page, run the Microsoft Script Debugger debugger tool to debug.
Select the open page 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 very useful function. When the code stops at a breakpoint, you can enter the variable name and press Enter to see the value of the variable at that time; the Command Window panel can even accept simple JavaScript Order. But the Microsoft Script Debugger debugger itself still has a bug problem.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn