Home > Web Front-end > JS Tutorial > body text

How to Retrieve Caller Function Line Number and Source URL in JavaScript?

Susan Sarandon
Release: 2024-10-20 21:55:29
Original
761 people have browsed it

How to Retrieve Caller Function Line Number and Source URL in JavaScript?

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>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!