Retrieving Caller Function Line Number and Source URL in JavaScript
Getting the caller function name is accessible using arguments.callee.caller.toString(). However, there are methods to further investigate the caller's details.
Caller Function Line Number
To obtain the line number where the function was invoked:
<code class="js">function getErrorObject() { try { throw Error(''); } catch (err) { return err; } } var err = getErrorObject(); var callerLine = err.stack.split('\n')[4]; var idx = callerLine.indexOf('at '); callerLine = callerLine.slice(idx + 2, callerLine.length);</code>
Caller Source URL
Unfortunately, obtaining the exact JavaScript source file or URL where the caller originated is not directly supported in JavaScript. However, in browsers like Chrome and QtWebView, you can access the call stack using the err.stack property, which provides a list of caller function names and line numbers. By analyzing this data, you may be able to infer the source file or URL.
The above is the detailed content of How to Retrieve Caller Function Line Number and Source URL in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!