views:

253

answers:

4

I've inherited a pile of code that's doing unexpected things when I click on some <button> elements.

The page has a lot of Javascript, and it's unclear what, if any, events have been setup on the button or its parent elements. I'm trying to find where execution starts so I can debug what's going on.

Is there a way to have Firebug (or any Javascript debugger) break whenever any javascript code is executed? Maybe someway to setup a break point on each line programmatically? Or is there some other way to find out which event the button is responding to (page users boh Prototype, jQuery, and some custom Javascript. I know.)

+4  A: 

Just press Pause button on the firebug panel (on the left-top corner of it, two yellow vertical lines, when Script tab selected) - and it will stops at first JS string.

But it will stop JS executing really at ANY event - so if you have a button which catches mouse move and mouse click - i will not be able to stop on mouse click, since script will be stopped at mouse move each time you try to move mouse over this button. I suggest you just to insert alert()'s in the suspicious places - after some iterations you will found exactly string you need (where error occurs).

In some cases you can get around the mouse move/click problem. For a button or any other element that can receive focus, you can tab to the element and press space or enter to fire the click event.
Matthew Crumley
Hmmm, +1 for good info, although clicking that button doesn't seem to be working on this particular page (which, javascript wise, is a bit of a mess). The button does its little glowing thing, but triggering javascript actions on the page doesn't kick the debugger to life.
Alan Storm
A: 

If this is a generic debugging problem you can use the Web Inspector in Safari / Chrome. An added bonus is that you can throw the following statement into your JavaScript code to pull up the debugger:

...
debugger; // Will trigger the debugger
...

Otherwise you can set breakpoints, conditional breakpoints, pause on the next execution, and all the usual tricks. Some nice videos are available here.

In response to the alert suggestion before. I would seriously recommend using console.log or console.dir if you have access to a debugger. You will get more information without blocking your script's execution. Also, don't forget that that console.log can take multiple parameters so you can nicely print out data, you don't have to constrain yourself to a single string like alert.

Joseph Pecoraro
Web Inspector doesn't solve this problem. THe undesired behavior isn't a halt in execution. The code is executing properly, but not doing what we want. I need to find the code that's triggered by the click.
Alan Storm
My comment was only a regurgitation of what was already stated, just with advantages the Web Inspector offers. I'm trying to point out that Firebug isn't the only answer. If you want to know what happens on click, take a look at the event listeners for the node (and parents) of where you click. This is available in the WebKit nightlies, and the Eventbug extension mentioned for Firebug.
Joseph Pecoraro
+1  A: 

I found a bookmarklet that will list all bound events in your page.

This kind be useful if you don't know what events are bound by third-party code.

Vincent Robert
+2  A: 

If you want to see what events are set up on the page, you might also try Eventbug.

Annie