JavaScript/Debugging
From Wikibooks, the open-content textbooks collection
Contents |
[edit] JavaScript Debuggers
[edit] Firebug
- Firebug is a powerful extension for Firefox that has many development and debugging tools including JavaScript debugger and profiler.
[edit] Venkman JavaScript Debugger
- Venkman JavaScript Debugger (for Mozilla based browsers such as Netscape 7.x, Firefox/Phoenix/Firebird and Mozilla Suite 1.x)
- Introduction to Venkman
- Using Breakpoints in Venkman
[edit] Internet Explorer debugging
- Microsoft Script Debugger (for Internet Explorer) The script debugger is from the Windows 98 and NT era. It has been succeeded by the Developer Toolbar
- Internet Explorer Developer Toolbar
- Microsofts Visual Web Developer Express is Microsofts free version of the Visual Studio IDE. It comes with a JS debugger. For a quick summary of its capabilities see [1]
[edit] JTF: Javascript Unit Testing Farm
- JTF is a collaborative website that enables you to create test cases that will be tested by all browsers. It's the best way to do TDD and to be sure that your code will work well on all browsers.
[edit] jsUnit
[edit] built-in debugging tools
In Firefox, open "Tools >> Javascript console". This displays errors and warnings for some common typos.
In Opera 9.5 and above, open "Tools >> Advanced >> Developer Tools" to open Dragonfly. This has many debugging tools such an error console and DOM veiwer.
Some people prefer to send debugging messages to a "debugging console" rather than use the alert() function. [2] [3] [4]
[edit] Common Mistakes
- Carefully read your code for typos.
- Be sure that every "(" is closed by a ")" and every "{" is closed by a "}".
- Remember that JavaScript is case sensitive. Look for case related errors.
- Don't use Reserved Words as variable names, function names or loop labels.
- Escape quotes in strings with a "\" or the JavaScript interpreter will think a new string is being started, i.e:
should bealert('He's eating food');alert('He\'s eating food');oralert("He's eating food");
- When converting strings to numbers using the parseInt function, remember that "08" and "09" (e.g. in datetimes) indicate an octal number, because of the prefix zero. Using parseInt using a radix of 10 prevents wrong conversion.
var n = parseInt('09',10); - Remember that JavaScript is platform independent, but is not browser independent. Because there are no properly enforced standards, there are functions, properties and even objects that may be available in one browser, but not available in another, e.g. Mozilla / Gecko Arrays have an indexOf() function; Microsoft Internet Explorer does not.
[edit] Debugging Methods
Debugging in Javascript doesn't differ very much from debugging in most other programming languages. See the article at Computer programming/debugging.
[edit] Following Variables as a Script is Running
The most basic way to inspect variables while running is a simple alert() call. However some development environments allow you to step through your code, inspecting variables as you go. These kind of environments may allow you to change variables while the program is paused.
[edit] Browser Bugs
Sometimes the browser is buggy, not your script. This means you must find a workaround.
[edit] browser-dependent code
Some advanced features of Javascript don't work in some browsers.
Too often our first reaction is: Detect which browser the user is using, then do something the cool way if the user's browser is one of the ones that support it. Otherwise skip it.
Instead of using a "browser detect", a much better approach is to write "object detection" Javascript to detect if the user's browser supports the particular object (method, array or property) we want to use.[5] [6]
To find out if a method, property, or other object exists, and run code if it does, we write code like this:
var el = null; if (document.getElementById) { // modern technique el = document.getElementById(id); } else if (document.all) { // older Internet Explorer technique el = document.all[id]; } else if (document.layers) { // older Netscape web browser technique el = document.layers[id]; }
[edit] For further reading
- "Javascript Debugging" by Ben Bucksch

