spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / venkman / 2 current pageTo page 2
[next]

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear

Debugging JavaScript Using Venkman, Part 2

If you're looking for more than a rudimentary debugger for your JavaScript code, look no further than Venkman. It's a full-fledged debugging environment incorporating tools that most programmers take for granted in other programming languages, and is designed to deliver cross-browser JavaScript compatible code. The best part is that it's freely available, and if you have a recent copy of Mozilla or Netscape on your system, you may already have it and not know about it.

The previous article looked at where you could find Venkman and some of its basic functionality. This article goes further and shows how you can actively use it in your JavaScript development.

Finding Out What's Wrong with Your Code

Let's say you've created some simple DHTML code, but it isn't working, and you haven't been able to figure out what's wrong. If you're surfing a page using Internet Explorer, you get a little yellow warning icon which, when you click on it, provides you with a simple analysis of what's wrong, and the line number at which your script breaks down. The problem with this; is that it's not always where the real problem lies (as anybody who has used this to debug their code more than a couple of times will know). While the code may fail at the point that Internet Explorer indicates, the real problem - a misnamed variable, an incorrect value being passed along, or something else - could just as well lie elsewhere, and probably is. This is where Venkman shines.

To get started with Venkman, go to the page you wish to inspect using a recent copy of Mozilla and then from the Tools menu select Development, and then JavaScript Debugger (the official name for Venkman). In the top-left window labeled Loaded Scripts you should see the URL for the page, along with any other linked scripts it finds on the page. Click the URL and it will reveal all of the JavaScript functions on the page. Each function is denoted by a little blue box, followed by the name for the function, and then the line number where it is referenced. Double-click on a function and Venkman will take you directly to the code referenced by that line number in the Source Code window. This is a handy way to locate the various functions in your code.

In order to start debugging, you need to switch out of this mode by clicking the Open Windows tab that appears alongside Loaded Scripts. This should also display the URL for your target page. Right-click on the URL and select Set as Evaluation Object. This targets the code on the page in the Interaction Session window, which should now show display the URL as the current selected context. To discover the values of various objects on this page, you can simply type in their corresponding JavaScript reference. No matter what page you're on, if you type in frames, you should get back [object Window] as a response, and if you have any images on your page typing in document.images ought to bring back [object HTMLCollection]. If you have any content, document.body ought to return [object HTMLBodyElement]. If you don't get the last message at the very least, something is seriously wrong with your Web page. ;-)

Using this you can change the state of the objects on this page for testing purposes. For example, if you wanted to play a somewhat mean trick on the browser, you can type in Navigator.appName = "Internet Explorer". You may get an error message at this point. If not, try typing in Internet Explorer and you will see that the browser dutifully reports back that it is no longer Mozilla but Internet Explorer. If you are looking at a page that does not actively use Navigator.appName = "Internet Explorer" you can expect an error message in your Interactive Session window, saying essentially that it is not accessible in this case. From this simple demonstration, you can get an idea as to the power behind Venkman and how it can be used.

Figure 1: Accessing the Navigator.appName value within a page in Venkman

Your Code Step by Step

One of the most useful features of Venkman is where you can stop JavaScript (from running), then visually step through its execution on the page. You can do this by clicking the Stop button (the one with the big red "X" on it), which stops the execution of any scripts currently running on the page. At this point you've likely run into a Catch-22 situation - since you're within Venkman, when you hit the button, the chances are good that your Web page has already executed all of the JavaScript on a page (unless of course you have a loop running, waiting for input from the user). Go back to your target page, hit reload, and you'll find that Venkman pops up immediately. Highlighted in yellow in the Source Code window is the first line of JavaScript on the page that was trying to execute, with much the same information displayed in the Interactive Session window. If you look back at your page in Mozilla, you'll see exactly where your page has stopped loading - if you've got JavaScript embedded in the header, you can expect a blank page. Back in Venkman you'll also notice that the Stop button is now inactive, and the buttons following it - Continue, Step Over, Step Into and Step Out - are now active. Hit the bright green Continue button and the rest of your Web page will load, displaying the end results in the Interactive Session window.

Figure 2: Stepping Through Halted Code in Venkman

The key to learning your way around the debugger are the three Step buttons, which guide you step-by-step through the JavaScript execution process on the page. The Step Into button takes you through the processing of a function line by line, Step Out takes you out of the current function and into the next, and the Step Over button jumps over the currently executed function and on to the next, if there is one. Using these buttons you can see exactly how - and more importantly, where - your functions are being executed on the page. Note that even when you step over the function, it is still executed - it is simply skipped over by Venkman in its display.


home / programming / javascript / venkman / 2 current pageTo page 2
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
How to Create an Ajax Autocomplete Text Field: Part 6 · Software Engineering for Ajax · Perl Pragma Primer
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Using File Virtualization for Disaster Recovery · VoIP Security: SIP—Versatile but Vulnerable · Around the World in 80 Nodes

Created: March 27, 2003
Revised: October 31, 2003

URL: http://webreference.com/programming/javascript/venkman/2