28

Is there a way to find out which Javascript function is fired by click on a certain button?

Let me explain ...
I have a web page with a large js script (not made ​​by me), I've to detect which function is triggered by a specific button, I need to find the function triggered by this button.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dee
  • 3,185
  • 10
  • 35
  • 39
  • have you got firebug installed? you can place break points on all your functions and the step through the code and work out whats happening – theedam Jun 19 '12 at 09:09

4 Answers4

28

Use Chrome's Web Inspector to inspect the element and check out the Event Listeners panel.

Web Inspector Example

If the code attaching the event is in a library (for example, by Zepto in my example), set a breakpoint on the line and examine the call stack to see where it originated in your code.

Call stack example

As you can see, the event has originated in my code and now I know the filename and line number.

alex
  • 479,566
  • 201
  • 878
  • 984
7

In Google Chrome, open your web page of interest.

Right-click on the element(i.e a button) you want to inspect. In the 'context menu' that appears, click 'Inspect'. A 'Developer tools' window will appear.

  1. Click the 'Elements' tab.
  2. Click the HTML element you wish to inspect if not already selected.
  3. Click on the 'Event Listeners' tab. A full list of listeners will show up.
  4. Select the event listener you're interested in. i.e 'click'. A call stack will show up.
  5. Click on the last item in the call stack. This will finally redirect you to that actual function that gets executed when the event fires.

Google chrome - Inspect option

Google chrome - Developer tools window

steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
3

Inside your functions, you can put arguments.callee to know about it. Notice that this is deprecated but should tell you what you are looking for.

alert(arguments.callee);
Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

Why don't you use Stack Trace for JavaScript, ie, stacktrace.js

Go to the above link, click on Bookmarklet and load the js file on any site you wish to inspect.

naveen
  • 53,448
  • 46
  • 161
  • 251