views:

1789

answers:

4

I need to set a breakpoint to certain event, but I don't know, where is it defined, because I've got giant bunch of minimized JS code, so I cant find it manually.

Is it possible to somehow set a breakpoint to for example click event of #registerButton, or find somewhere which function is bound to that event?

I found Firefox addon Javascript Deobfuscator, which shows currently executed JS, which is nice, but the code I need to debug is using jQuery, so there's loads of function calls even on the simplest event, so i cant use that either.

Is there any debugger made especially for jQuery?

Does anybody know some tool that turns minified JS back into formatted like turn function(){alert("aaa");v=3;} back into

function() {
   alert("aaa");
   v = 3;
}
+1  A: 

You can use the debugger command anywhere in a javascript file. When the interpreter hits that statment, if a debugger is available (like Firebug) then it gets triggered

Gareth
The problem is that I don't know where in the code is the event defined. I need debugger to tell me that, for example by showing the function that is bound, or setting a breakpoint to the event without needing to know, where is it in the code.
Darth
+2  A: 

First replace minified jquery or any other source you use with formated. Another useful trick I found is using profiler in firebug. The profiler shows which functions are being executed and you can click on one and go there to set a breakpoint.

Sergej Andrejev
Yep, but profiler isn't very convenient when used with jQuery.
Darth
Then use non-minified version. What is the problem?
Sergej Andrejev
The problem is that I don't have non-minified version of all sources.
Darth
A: 

find the line and place a breakpoint. Then reload the the site / page. Then firebug will automatically pause the execution of the statement when a breakpoint is hit.

+5  A: 

Well it might all be too much trouble than it's worth, but it looks like you have three things to do:

  1. De-minify the source. I like this online tool for a quick and dirty. Just paste your code and click the button. Has never let me down, even on the most funky of javascript.

  2. All jQuery event binders get routed to "jQuery.event.add" (here's what it looks like in the unbuilt source), so you need to find that method and set a breakpoint there.

  3. If you have reached this far, all you need to do is inspect the callstack at the breakpoint to see who called what. Note that since you're at an internal spot in the library you will need to check a few jumps out (since the code calling "jQuery.event.add" was most likely just other jQuery funtions).

Note that 3) requires Firebug for FF3. If you are like me and prefer to debug with Firebug for FF2, you can use the age-old arguments.callee.caller.toString() method for inspecting the callstack, inserting as many ".caller"s as needed.


Edit: Also, see "How to debug Javascript/jQuery event bindings with FireBug (or similar tool)".

You may be able to get away with:

// inspect    
var clickEvents = jQuery.data($('#foo').get(0), "events").click;
jQuery.each(clickEvents, function(key, value) {
    alert(value) // alerts function body
})

The above trick will let you see your event handling code, and you can just start hunting it down in your source, as opposed to trying to set breakpoint in jQuery's source.

Crescent Fresh