How to Get JavaScript Caller Function Line Number and Caller Source Url

How to get JavaScript caller function line number and caller source URL

This works for me in chrome/QtWebView

function getErrorObject(){
try { throw Error('') } catch(err) { return err; }
}

var err = getErrorObject();
var caller_line = err.stack.split("\n")[4];
var index = caller_line.indexOf("at ");
var clean = caller_line.slice(index+2, caller_line.length);

How to get filename and line number of where a function is called in Node?

You can create an Error to get where the Error is, and its stack trace. Then you can put that into a function, to get the line where it is.

function thisLine() {
const e = new Error();
const regex = /\((.*):(\d+):(\d+)\)$/
const match = regex.exec(e.stack.split("\n")[2]);
return {
filepath: match[1],
line: match[2],
column: match[3]
};
}

console.log(thisLine());

This works for me in Google Chrome.

And also in node.

Note to @j08691's comment:

Both this and this seem to be using lineNumber, which is not present (as far as I could test) in NodeJS.

return the line number in JavaScript

Skip the try / catch and just use the Error object:

var x = new Error("I want the line number");
console.log(x.lineNumber);

More information is available at the MDN Docs

Also note that proprties like lineNumebr are implemented in specific interpreters and is not universal across all browsers.

Get name and line of calling function in node.js

Using info from here: Accessing line number in V8 JavaScript (Chrome & Node.js)

you can add some prototypes to provide access to this info from V8:

Object.defineProperty(global, '__stack', {
get: function() {
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack) {
return stack;
};
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
});

Object.defineProperty(global, '__line', {
get: function() {
return __stack[1].getLineNumber();
}
});

Object.defineProperty(global, '__function', {
get: function() {
return __stack[1].getFunctionName();
}
});

function foo() {
console.log(__line);
console.log(__function);
}

foo()

Returns '28' and 'foo', respectively.

How do you find out the caller function in JavaScript?

Note that this solution is deprecated and should no longer be used according to MDN documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller



function Hello()
{
alert("caller is " + Hello.caller);
}

Note that this feature is non-standard, from Function.caller:

Non-standard

This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.


The following is the old answer from 2008, which is no longer supported in modern Javascript:

function Hello()
{
alert("caller is " + arguments.callee.caller.toString());
}

Finding the Source Line of a function call

Having written a logging library (log4javascript) myself, I've considered this same problem and here are my thoughts:

The problem is that in order to get the information you want, you need an Error object that was created on the line in question. Creating an Error within your logging utility will only directly give you the filename and line number for the particular line in your logging utility code rather than for the line of code that made the logging call. The only way round this I can think of is parsing the stack property of the Error (or the message property in Opera), which has several problems:

  • the stack trace is only available in Mozilla, recent WebKit and Opera browsers
  • the stack trace is a string that varies from browser to browser, and may change format again without notice in future browsers, thus breaking the parsing code
  • throwing an Error and parsing its stack trace for every log call will add a significant performance overhead.

For the purposes of log4javascript, I decided it wasn't worth implementing, but for your own needs you may decide it's worthwhile.



Related Topics



Leave a reply



Submit