
Beginning JavaScript With DOM Scripting And Ajax - From Novice To Professional (2006)
.pdf
466 |
A P P E N D I X ■ D E B U G G I N G J A V A S C R I P T |
Opera 8.5
Opera not only comes with options to quickly turn off all kinds of browser support (JavaScript, cookies, plug-ins, pop-ups) by pressing F12, which helps immensely to test whether your web page is dependent on JavaScript or not, it also has a built-in JavaScript console that gives very detailed error reports. You can turn on the console via Tools Advanced JavaScript console as shown in Figure A-7.
Figure A-7. Showing the JavaScript console in Opera
Firefox 1.5.0.3
Firefox stays silent and doesn’t bother the visitor by reporting JavaScript errors. However, it comes with a very powerful JavaScript console, which you can show by selecting Tools JavaScript console on the main toolbar as shown in Figure A-8.

A P P E N D I X ■ D E B U G G I N G J A V A S C R I P T |
467 |
■Note Don’t be confused if your Firefox browser doesn’t have the same options as shown in Figure A-8; some of them are dependent on extensions I installed. However, you should have the JavaScript console.
Figure A-8. Showing the JavaScript console in Firefox
Once activated, the JavaScript console is visible in its own browser window, which lists the JavaScript errors that occur in any page you open in any of your Firefox windows or tabs. It is quite amazing to see how many errors accumulate when you surf the Web a bit with the console open. The error in exampleMissingCurly.html would display as shown in Figure A-9.
Figure A-9. The Firefox JavaScript console showing an error
You can either show errors, warnings, and messages, or filter the list for each of them. Clicking the Clear button will wipe the list clean. You can even evaluate any JavaScript by pasting it into the code box and clicking the Evaluate button.

468 |
A P P E N D I X ■ D E B U G G I N G J A V A S C R I P T |
By default, the JavaScript console will open in a new window, but there is a trick you can use to show it in the sidebar instead.
1.Make sure the bookmarks toolbar is visible by selecting View Toolbars Bookmarks Toolbar.
2.Right-click the toolbar and select New Bookmark.
3.Add the information shown in Figure A-10. The location must be chrome://global/ content/console.xul, but you can name the bookmark anything you like.
4.Make sure the Load this bookmark in the sidebar option is activated.
This will add a new bookmark button to the toolbar that opens the JavaScript console in the sidebar when you click it.
Figure A-10. A trick to make Firefox show the JavaScript console in the sidebar
This should get you started in spotting errors quickly in Firefox; however, it is only the tip of the iceberg when it comes to great helpers available for this browser. Mozilla and Firefox can be easily improved with extensions written in JavaScript and an XML-based language called XUL.

A P P E N D I X ■ D E B U G G I N G J A V A S C R I P T |
469 |
Developers keep churning out new extensions and putting them on the Web. Two of those are very handy for you as a JavaScript developer:
The Web Developer Extension
The Web Developer extension is a toolbar by Chris Pederick that is available at http:// www.chrispederick.com/work/webdeveloper/. This toolbar allows you to quickly toggle JavaScript support (which you can do otherwise via Tools Options Content Enable JavaScript), show HTML generated by JavaScript in the Source Viewer, populate forms automatically with data, edit the page’s style sheet on the fly, and choose from many more options. It is a “Swiss Army Knife” for web development.
FireBug
If you thought the JavaScript console of Opera, Safari, and Firefox was handy, get the FireBug extension by Joe Hewitt (who is also responsible for the DOM inspector that is part of the default Firefox installation) at http://www.joehewitt.com/software/firebug/. The FireBug extension is visible in the bottom right of the browser window and either shows a warning icon with the number of errors in the console or a green icon indicating that everything is OK. If there are errors, you can click the icon (or press F12) to open the FireBug console in the bottom half of the browser window as shown in Figure A-11.
Figure A-11. The FireBug extension with open console
The features of FireBug are too many to mention here. Whatever is happening to the document, you can see it in the console. It shows the DOM structure, the applied CSS, the dimensions and properties of elements, events assigned to elements, the return values of Ajax requests, and much, much more. In the DOM inspector mode, you are even able to change the document on the fly to test changes quickly in a remote document. This is for testing purposes only—you cannot save the changes on the remote server, of course.
470 |
A P P E N D I X ■ D E B U G G I N G J A V A S C R I P T |
Venkman
Mozilla’s answer to the Microsoft Script Debugger is called Venkman and is available at https:// addons.mozilla.org/firefox/216/. Venkman is a full debugging suite that allows you to watch variables, set debugging points, and really go down into the depths of JavaScript development. Explaining all the features of Venkman would fill its own chapter, and for day-to-day JavaScript tasks, it may be overkill to use. If you want to know all the features of Venkman and how to use it, check the walkthrough provided on the homepage at http://www.mozilla.org/projects/ venkman/venkman-walkthrough.html. Another good start is the introductory article “Learning Venkman” by Svend Tofte, available at http://www.svendtofte.com/code/learning_venkman/. The newest versions of the aforementioned FireBug extension also include a debugger like Venkman, which makes it obsolete in some way; however, it is worth mentioning as it is in use in some companies.
JSLint and JSUNIT
Some browser-independent tools are available that could also help you with JavaScript development. One is the online JavaScript verifier JSLint by Douglas Crockford, available at http:// www.jslint.com/lint.html. JSLint is a tool written in JavaScript that verifies scripts in terms of syntactical validity and tries to ensure good coding style. As “good coding style” is a subjective matter, you may want to take JSLint reports with a grain of salt. JavaScript development happens mostly in browser environments, and from time to time you need to bend the rules or cut corners to make a script work faster or work around browser issues. That being said, it is still a great tool to find ways to optimize your scripts in terms of cleanliness of code if you don’t have to cut corners. The great thing about JSLint is that it gives you a full analysis of your script, including reports on global variables and how many times different functions have been called.
If you come from a back-end coding environment, you may have already used or at least heard about unit testing (http://en.wikipedia.org/wiki/Unit_testing). In a very small nutshell, unit testing means that you write test cases for all your methods and functions, and a “testing harness” allows you to run all these tests in succession when you press a button. This way you can ensure that your code works as you define the test cases it has to fulfill before you develop the code. For Java, there is JUnit; for PHP, there is PHPUnit; and yes, for JavaScript there is JSUnit, available at http://www.jsunit.net/.
Summary
All in all, debugging JavaScript is much easier these days than it was some years ago, and especially in the Firefox extension world it is very important to keep your eyes open, as new products that you may profit from are released almost weekly. The renaissance of JavaScript as part of the Ajax methodology has also made development IDE vendors a lot more aware of the need for good JavaScript debugging tools. Products that were traditionally meant exclusively for Java or .NET development are beginning to add support for JavaScript that goes beyond color-coding the sources.


