Catching the specific javascript code being executed onClick

大憨熊 提交于 2019-12-18 18:38:11

问题


I am working on a site that has loads of legacy javascript and jquery includes and there is no documentation to show what is happening when.

I have a specific problem to fix and I cannot find the relevant code that is being executed when a button is clicked. To save me from trawling through (and making sense of) hundreds of lines of legacy script, is there a feature, possibly in Firebug, that will identify what script is being executed when I click on a button?

Thanks for your help.


回答1:


I believe there is a feature in Firebug's console window called Profile. Click profile, click the button, then click profile again. It should give you what all functions were called in that time. Be warned that if this code includes jQuery, you might get a huge long list of functions because jQuery uses tons in its code. Unfortunately, the profiler will also show anonymous functions, which can really be a pain.

Otherwise, do a search in the code for the button's class or ID and go through them. If you have an id of fancy then you might do a search for #fancy in your code and attempt to find it. That may lead you in a general direction, at least.




回答2:


You can click Firebug's "Break on next" button (in the Script tab; it looks like a pause button), then push the button that you want to debug.

The next time any JavaScript code executes, Firebug will break into the debugger and show you that line of code.




回答3:


The break button didn't work for me. Instead I did edit the onclick attribute with FireBug and prepended it with "debugger;" ... then you'll break right there once you click :)




回答4:


None of the above answers worked for me. I am trying to use Firebug to figure out how a feature on a page is working for a site I have no control over. Here is what worked for me.

First, got the id of the element I am clicking on from the page source, and then get a temporary reference to it by creating a watch (under the script tab):

tmp=document.getElementById("idOfElement")

Next, I assigned the current onclick value to another temporary variable.

oldfunc=tmp.onclick

Next, I defined a new onclick function. Initially I tried putting debugger; as the first thing in the function, but this does not work! So instead, I created an alert:

tmp.onclick = function() { alert("Ok"); oldfunc() }

Now, as soon as I click on the button the alert comes up, at which point I then click the "Break on next" button as outlined in another answer to this question. Then I dismiss the alert and immediately I am in the debugger at the correct place.

In my case, the "Break on next" button did not work by itself, because there are a lot of other events, just mousing over the page was causing the breakpoint to be hit, preventing me from ever clicking the button.




回答5:


In Firebug you can set a breakpoint in some JS and then you get a stack which will let you know the current function call stack. So if you set the breakpoint in function used by several handlers then you can use this to discover exactly which handler you are in.

This probably won't work if you are dealing with AJAX callbacks and the like though.



来源:https://stackoverflow.com/questions/2058103/catching-the-specific-javascript-code-being-executed-onclick

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