![]() |
Site Archive (Complete) | |||
|
ABOUT US |
CONTACT |
ADVERTISE |
SUBSCRIBE |
SOURCE CODE |
CURRENT PRINT ISSUE |
NEWSLETTERS
|
RESOURCES
|
BLOGS
|
PODCASTS
|
CAREERS
|
||||
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
|
|||||||||||||||||||||||||
|
MEDIA CENTER
NetSeminar
Fully Utilize Embedded Hardware Potential and Create Advanced User Interfaces Fast Processing power and display capabilities previously limited embedded system development. Now larger, full-color displays and powerful hardware running advanced operating systems are affordable. To take advantage of this new opportunity high level programming is required. Learn how with a complete set of tools, such as Trolltech's C++ development framework, fully utilizing hardware potential and creating advanced user interfaces can be achieved quickly and efficiently. Event Date: Wednesday, May 28, 2008
Amp Up Your Continuous Integration Builds
Amp up your continuous integration build process by increasing the frequency of your build loop. Learn from experts how to create a build that can quickly adapt to source code changes and drive down build times from hours to minutes. Event Date: Thursday, May 22, 2008
Become a Better Build Detective: Effective Techniques for Debugging Makefiles
This Webcast will uncover some of the most common Makefile errors and provide specific, proven techniques for fixing them. If command or syntax errors have been a mystery for you, this is a discussion you won't want to miss. Event Date: Wednesday, May 28, 2008
|
DEPARTMENTS
♦ sponsored |
|
EVENTS
![]() July 21-24, 2008 Chicago, IL Find real-world solutions to your biggest software architecture challenges at Architecture & Design World 2008. Register by June 20 and save up to $300! ![]() THE DOBBS CHALLENGE Download the Dr. Dobbs Challenge game for either Windows or Windows Mobile and modify it using Visual Studio 2008. Win $10,000!
INFO-LINK
Resource Links: |
|
|
|