Debugging JavaScript events with Firebug

╄→尐↘猪︶ㄣ 提交于 2019-11-29 21:35:38
Crescent Fresh

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.

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.

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.

What you could do is get the minified code - you will have access to this. De-minify the code as per Crescent Fresh's (1) option or any method you prefer.

Then download and install Fiddler - all web devs should have this incredible tool installed.

Then using Fiddler you can intercept the browser's call for the minified.js file with the local unminified.js you have on your local drive.

Fiddler basically intercepts your browser's request for a specific file and can serve up a different file to your browser. So you can now look at un-minified code.

Easy.

Another option is to use Firefox and get the Venkman debugger installed on it - far better than Firebug IMHO - and the Venkman debugger has a "pretty print" option which can visually un-minify the minified code at run time. Stick your break point wherever you want now...

Jonathan Sayce

Another update: There's now a firebug extension that beautifies JavaScript:

https://addons.mozilla.org/en-US/firefox/addon/javascript-deminifier/

It's working perfectly for me in Firefox 12.0.

Credit to this answer for spotting it.

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

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.

Alexander

This will do the job. http://jsbeautifier.org/ You can also search Google for a javascript beautifier.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!