Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging JavaScript events with Firebug

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 JavaScript code, so I can't find it manually.

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

I found the Firefox add-on Javascript Deobfuscator, which shows currently executed JavaScript, 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 can't use that either.

Is there any debugger made especially for jQuery?

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

function() {
   alert("aaa");
   v = 3;
}
like image 428
Jakub Arnold Avatar asked Apr 05 '09 11:04

Jakub Arnold


People also ask

Is there a debugger for JavaScript?

Using debugger The debugger keyword stops the execution of the code and calls the debugging function. The debugger is available in almost all JavaScript engines. Let's see an example, let a = 6; let b = 9; let c = a * b; // stops the execution debugger; console.


3 Answers

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 functions).

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.

like image 165
Crescent Fresh Avatar answered Oct 25 '22 21:10

Crescent Fresh


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.

like image 4
Sergej Andrejev Avatar answered Oct 25 '22 19:10

Sergej Andrejev


Just an update:

Google Chrome (the dev tools in it) support all sorts of breakpoints, along with break on Event.

All events together with device orientation changes and timers

You can see a video with a presentation of all the capabilities from Google IO.

There's also built-in deminifier/ unminimizer/ prettifier which will help you set breakpoints manually on compressed javascript files.

like image 3
antitoxic Avatar answered Oct 25 '22 19:10

antitoxic