I have some console.log prints that are triggered in a specific javascript file. This file is aware of other files, and it make references to those filenames in the console.log prints. I was wondering if there was a way I could print some sort of link to that source file in the same console.log print out, with the hopeful behavior that clicking the link would open the source view of that file in dev tools?
Asked
Active
Viewed 1,192 times
2 Answers
3
Found out by trying out @miir's solution that you can:
1) Link to a file as long as you put the full path
console.log("http://localhost:8000/myapp/path/to/file.js")
console.log(document.currentScript.src); // if executed from within a script
2) Link to a file + line + character manually
const line = "83";
const character = "2";
console.log(document.currentScript.src + ":" + line + ":" + character);
3) Link to a file + line + character dynamically
const level = 1; // 1 is last stack trace, you can go up the calling functions
console.log(new Error().stack.split("\n")[level].match(/http[^\)]+/)[0]);
Nothing that couldn't be figured out from the accepted answer, but maybe it can help newcomers.

Julian Honma
- 1,674
- 14
- 22
0
At the time of this answer, there is no direct way to do this.
However, @Emissary points out in the comment below, a work-around of accessing a link to a file is by taking advantage of the Error object's ability to do so.
console.log(new Error().stack)

miir
- 1,814
- 1
- 17
- 25
-
1not strictly true, you can work around it by parsing / printing the stack trace from an `Error` object. ***e.g.*** primitively, `console.log(new Error().stack)` - or by checking out a more [robust wrapper in this answer](http://stackoverflow.com/a/14842659/1238344). – Emissary Mar 24 '16 at 21:37
-
@Emissary I see! Thank you! – miir Mar 29 '16 at 21:22