JavaScript
  Home arrow JavaScript arrow Page 2 - Debugging in Javascript
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Debugging in Javascript
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2005-02-16

    Table of Contents:
  • Debugging in Javascript
  • The Hard Way
  • Using Comments
  • Are We There Yet?
  • Make a Log Window

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Debugging in Javascript - The Hard Way


    (Page 2 of 5 )

    If there is a situation where you can't use Mozilla or Venkman for your development, you will have to do things the hard way. Internet Explorer and Firefox both have a way to display Javascript errors, which will sometimes help. Apple's Safari also has this feature, using a separate application. Neither browser will help you if there are no syntax errors in your code. If you are trying to figure out why the wrong data is being written, or a number isn't right for instance, you're out of luck.

    The most common method for debugging code in the browser is to use the alert box. It can be used to display variable contents, object names and types and loop counters. Unlike the way a debugger works however, you must write the alert function into your code.

    An Example

    For instance, say you are trying to determine if you have a comment before a paragraph and if you do, you want to add a second comment in the source before the paragraph. For some reason the "if" construct is not evaluating. Use the alert() function to bring up a dialog box that shows you the value for which you are supposed to be checking.

    function process(pName,addedText)
    {
       var paragraph = document.getElementById(pName);
       alert(paragraph.previousSibling.nodeName);
       if(paragraph.previousSibling.nodeName == "comment")
       {
          paragraph.parentNode.insertBefore(document.createComment(text),paragraph);
       }
    }



    So what's the problem? Well, as you can see in the screen shot, the dialog shows that the nodeName property for a comment tag in this browser (Firefox) is "#text" which means it is being parsed as a text node. You might not know this if you're used to working with the nodeName properties of other tags, such as a paragraph tag, which is simply "p." if you changed the if construct to look for "#text" it would evaluate properly.

    Please note that, curiously, if you create a comment as we did above and check its node name property, it will come up as "#comment." Only comments that were in the source to start with come up as "#text," if indeed they come up at all. Comments are a node type, but they are apparently not parsed as such by most user agents when the document is loaded.

    More JavaScript Articles
    More By Chris Root


       · Welcome everyone. Thank you for reading the article.
       · Good article. The popup window with updating values is a great way to watch the...
       · log4javascript (http://www.timdown.co.uk/log4javascript) has excellent pop-up...
       · I was having problems debugging a problem, that only happened on a live site - the...
       · Think I forgot to supply the link... doh! ;-)Here it is:...
       · You say one should name the file for the log window "dialog.html", but you're using...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT