[thelist] Debugging javascript

George Dillon George Dillon" <george.dillon at ukonline.co.uk
Sun May 14 09:29:51 2000


From: Michael Galvin


> I need to find out the value of used variables at
> certain points in the script.  Is there a sort of VB
> msgbox function or something I could use?

Simple... use a JS alert at those key points:

alert('variableIwanttoknowabout='+variableIwanttoknowabout)

or

alert('varIwtka1='+varIwtka1+', varIwtka2='+varIwtka2)

If you've got lots of these, you can watch the progress of execution
of a script and not only track the variable values but discover
exactly where the script breaks (for those occasions when IE
misreports the line number), but to do so you'll probably want to
number your alerts, and/or make them show which function they are in:

alert('alert# functionname  varIwtka='+varIwtka)

When you think you're done debugging, comment the alerts out:

// alert(...etc...)

...and then when you're absolutely positive it's all working and
tested x-browser and you'll never need them again you can delete them
all... but you might consider leaving them all in situ as comments.
If you're ever likely to tinker with the script later you may regret
deleting them.

You could also try asking Jeff to deliver his long-promised parts 2
and 3 of his JavaScript Debugging article (although in my experience
this doesn't work ;)

Part 1 is at:
http://www.evolt.org/index.cfm?menu=8&cid=472&catid=17

HTH

George