flea whale
flea whale

Reputation: 1803

Chrome JS debugging help... where was function called?

I'm using console.log(var_name); to debug JS code in chrome and am wondering, is there a way to output to the console the line at which the current function was called?

Upvotes: 1

Views: 142

Answers (2)

pimvdb
pimvdb

Reputation: 154838

You can get the stack trace with the following:

(new Error).stack;

You could then use a regular expression to filter out the line number: http://jsfiddle.net/8wA74/.

var stack = (new Error).stack.split("\n"),
    line = /:(\d+):/.exec(stack[1]); // 1 is stack depth, format is "url:line:char"

console.log("Current function was called on line " + line[1]);
// this 1 is the number of the group in regexp

Upvotes: 2

TJHeuvel
TJHeuvel

Reputation: 12608

Not that i know of, but wouldnt it be possible to set a breakpoint instead? That surely has a stacktrace visible.

Just try to click the linenumber in the dev. console, it'll show a blue arrow and next time it hits it'll show you the stacktrace.

Upvotes: 1

Related Questions