How can I find out in a javascript function which was the calling (the former in the call stack) function?
I would like to determine if the former called function is a __doPostback in the onbeforeunload event.
How can I find out in a javascript function which was the calling (the former in the call stack) function?
I would like to determine if the former called function is a __doPostback in the onbeforeunload event.
Each function has a caller
property defined.
From https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function/caller:
function myFunc() {
if (myFunc.caller == null) {
return ("The function was called from the top!");
} else
return ("This function's caller was " + myFunc.caller);
}
}
The Function.caller
property is not part of the ECMA3 standard but it's implemented across all major browsers, including IE and Firefox.
If you're using an anonymous function, you can still access the caller
property via the arguments.calee
property:
function() {
if (arguments.callee.caller == null) {
return ("The function was called from the top!");
} else
return ("This function's caller was " + arguments.callee.caller);
}
}
Note that this code is accessing the current function, and then referencing the same non-standard caller
property on it. This is distinct from using the deprecated arguments.caller
property directly, which is not implemented in some modern browsers.