January 01, 2002
Debugging JavaScriptMakiko Itoh
Whether you are cutting-and-pasting pre-written scripts, just starting out in writing your own, or are a seasoned JavaScripter, you probably have to spend a lot of time troubleshooting and debugging. I've found that this part of the scripting process can be challenging regardless of your level of JavaScript experience. To make life easier, here are some fundamentals of debugging and a few troubleshooting steps just for you.
Understanding Different Types of Errors
There are three basic types of errors in JavaScript, or any other programming or scripting language for that matter. They are Syntax errors, Runtime errors, and Logical errors. Whenever you're troubleshooting a script, try to look for errors in that order, starting with Syntax errors. It's also important to know what each of the error messages mean.
Getting a Programmer's Text Editor
All too often I see people struggling to hand-code in a plain text editor like Notepad or SimpleText. Yes, you can write code with them. But why make your life harder when there are so many free or inexpensive text editors available? Even if all you want to do is hack other people's scripts, you'll need a text editor. And most of these text editors also make your HTML coding tasks easier, too. A good text editor should offer these features:
The screenshot here shows how useful this can be. With color-coding, it's easy to spot a syntax errorin this case, a missing quotation mark.
Programmer's editors are easy to find. In fact, Web Review's David Mertz writes an ongoing series on programmer's editors, which you can find detailed on his bio page.
Using Netscape 4's Built-in JavaScript Debugger
Those cryptic JavaScript error messages in MS Internet Explorer, or Opera 4, for that matter, such as "Object Expected" or "Undefined Function" are rather unhelpful. In Netscape Navigator or Communicator 4.x, you will also see the even less helpful "JavaScript error" in the status area while your page stops dead in its tracks. But the built-in JavaScript debugger can pinpoint many common errors, especially the most obvious syntax errors. To invoke it, just type "javascript:" (without the quotes) in the location of your Netscape browser. This will open a debugging console window that shows all of your most recent JavaScript errors. To clear the console, just click the "Clear" button. Besides running your code, you can also type or copy/paste commands directly into the debugger text area. (Note: Mozilla/Netscape 6 doesn't have this built-in debugger yet.)
Here is a screenshot of a portion of the Netscape 4 debugger:
Making it Simple
Sometimes, you may slowly build up a function, until you have lines and lines of code, with many nested conditional statements, and then completely lose track of what you're doing. The best way to retrace your steps is to comment out pieces of your code until you get back to a place where things are actually working. The term commenting out means placing special characters at the start of a line or around a piece of text, so that the script skips over it.
There are two ways of adding comments to JavaScript: Putting two slashes // at the beginning of a line or a portion of text, or surrounding your comments with /* and */. I recommend using the second method for all but the shortest bits of text, since sometimes the browser will wrap the text at an arbitrary place in your line. You can comment out whole chunks of nonworking text simply by surrounding it with /* */.
Inserting
|
|||||||||||||||||||||||
|
RELATED ARTICLES
No Related Articles
|
TOP 5 ARTICLES
No Top Articles.
|
|
MEDIA CENTER
NetSeminar
Modernize your Development by Moving Build and Code Quality Upstream Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible. The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast. Date: Wednesday, July 15, 2009 Time: 11 am PT/2 pm ET
Modernize your Development by Moving Build and Code Quality Upstream
Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible. The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast. Date: Wednesday, July 15, 2009 Time: 11 am PT/2 pm ET
|
DEPARTMENTS
|
|
|
|
|