the only thing i know how to javascipt is using alert(). is there any other way to help debugging javascript ?
views:
122answers:
7Using Firebug, and the web development tools on other browsers, you can use commands such as
console.log(myVar);
to watch for variables, instead of the more disruptive alert
. With the consoles on most development tools, you can also run arbitrary pieces of Javascript from there without having to modify the actual code, which can help immensely with with debugging Javascript. Oh, and the break on error function helps too.
When you open up Firebug you see something like this. Click on the console tab and enable it:
Enter your code after the >>>
. Just for fun try something like $('body').css('-moz-transform', 'rotate(10deg)')
:D
Most modern browsers have such capabilities already inside or with some extra plugins:
- Internet explorer 8 Developer toolbar
- Firefox/firebug
- Built in developer tools in Chrome and Safari
These are all free tools available to you and work great when profiling/debugging javascript.
If you can't use any of these browser tools (for whatever reason, maybe mobile development?), you could also attach YUI Logger to your page, which pops up a debugging console you can write to: http://developer.yahoo.com/yui/logger/
Firebug is the favorite option when debugging Javascript.
However another alternative often missing from the answers, Opera Dragonfly, is worth the try.
Opera is one of the most compliant browser (to standards), and its debugging tools are not only nice, they're also very powerful. Opera is free.
There are various logging tools available, including my own log4javascript, which works on all major browsers.