且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Firebug调试JavaScript事件

更新时间:2023-12-05 10:40:46

麻烦很多,但是看起来你有三件事要做:


  1. 我喜欢这个在线工具,快速而肮脏。只需粘贴你的代码,然后点击按钮。没有让我失望,即使是最时髦的JavaScript。


  2. 所有 jQuery事件绑定器路由到jQuery.event.add这里是未完成的源代码),因此您需要找到该方法并在其中设置断点。


  3. 如果您有到目前为止,你需要做的就是在断点处检查callstack,看看谁叫什么。请注意,由于您在图书馆的内部位置,您需要检查几个跳出(因为代码调用jQuery.event.add最有可能只是其他的jQuery函数)。


请注意,3)需要Firebug for FF3。如果您喜欢我,并且喜欢使用Firebug for FF2调试,可以使用古老的 arguments.callee.caller.toString()方法来检查callstack,插入根据需要调用。






编辑:另请参阅如何使用FireBug(或类似工具)调试Javascript / jQuery事件绑定



您可能可以获得离开:

  //检查
var clickEvents = jQuery.data($('#foo')。get (0),事件)点击;
jQuery.each(clickEvents,function(key,value){
alert(value)// alert function body
})

上述技巧将让您看到您的事件处理代码,您可以在 中开始搜索>源代码,而不是在jQuery的源代码中设置断点。


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;
}

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.