In any programming language, I can trace any function and know which function is called by other. But in Javascript , I don’t know how, since the code is not written by me and Firebug does not give this feature – as far as I know.
An example :
I want to display the function names of each function that is called when clicking on XYZ Element, and display them in order.
Advertisement
Answer
Found this: A javascript stacktrace in any browser, James says they have a github account now
JavaScript
x
52
52
1
function printStackTrace() {
2
var callstack = [];
3
var isCallstackPopulated = false;
4
try {
5
i.dont.exist+=0; //doesn't exist- that's the point
6
} catch(e) {
7
if (e.stack) { //Firefox
8
var lines = e.stack.split('n');
9
for (var i=0, len=lines.length; i<len; i++) {
10
if (lines[i].match(/^s*[A-Za-z0-9-_$]+(/)) {
11
callstack.push(lines[i]);
12
}
13
}
14
//Remove call to printStackTrace()
15
callstack.shift();
16
isCallstackPopulated = true;
17
}
18
else if (window.opera && e.message) { //Opera
19
var lines = e.message.split('n');
20
for (var i=0, len=lines.length; i<len; i++) {
21
if (lines[i].match(/^s*[A-Za-z0-9-_$]+(/)) {
22
var entry = lines[i];
23
//Append next line also since it has the file info
24
if (lines[i+1]) {
25
entry += " at " + lines[i+1];
26
i++;
27
}
28
callstack.push(entry);
29
}
30
}
31
//Remove call to printStackTrace()
32
callstack.shift();
33
isCallstackPopulated = true;
34
}
35
}
36
if (!isCallstackPopulated) { //IE and Safari
37
var currentFunction = arguments.callee.caller;
38
while (currentFunction) {
39
var fn = currentFunction.toString();
40
var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf('')) || 'anonymous';
41
callstack.push(fname);
42
currentFunction = currentFunction.caller;
43
}
44
}
45
output(callstack);
46
}
47
48
function output(arr) {
49
// Output however you want
50
alert(arr.join('nn'));
51
}
52