0

I need to know if there is a way to extract the script name when calling a variable or a function in javascript. The scenario is running chrome through webdriver or node and when calling for example window.jQuery.Animation I would like to get the filename (i.e., jquery.js) where this function is defined. I know in chrome you can click on 'show function code' and jump to the file, however I am looking to do the same in a programmatic way.

Thanks

1
  • That's not easily done unless the script records that information at load time. Even then it's not always reliable, i.e., if the script is marked defer or async. Commented Dec 19, 2015 at 19:30

2 Answers 2

1

Maybe an Error object can help here:

var e = new Error;
console.log(e.stack);

The first two lines of the stack:

Error
at Object.<anonymous> (/home/joost/src/test/testfile.js:3:9)
Sign up to request clarification or add additional context in comments.

3 Comments

How can that be helpful? The question is "How to find where say jQuery's $ or lodash's _ is defined ?". Here your error only traces where the error is actually defined, it does doesn't help you query the declaration of a method.
I've used it to modify some code where I wanted to figure out what was calling it. Reading the question again, it looks like that's actually the reverse of what the author wants, so I guess it's actually not useful at all!
I love this suggestion, but I think it depends on having control of the function. You need to throw the error in the function whose source path you're interested in. A ninja might be able to think of a way of calling an arbitrary function such that it throws?
0

You have no such way of doing that in pure JavaScript.

The different javascript engines (spiderMonkey, V8, ...) will often compile the javascript code in some way, so you will lose such information in the process.

To know where a function comes from, you will require this function's source map. If you don't know them, source maps will map a script to it's originating filename / line number.

The mozilla sourceMap library will help you with that.

Though, you will have to generate a source map for your file that uses jQuery as well.

if your script.js is like

var a = jQuery.method // this is line 10
        ^ col 9

You will have to execute

var sourceMap = require('source-map');

consumer = new SourceMapConsumer('script.map');

consumer.originalPositionFor({ line: 10, column: 9 })

// { source: 'jQuery.js',
//   line: 12971,
//   column: 222,
//   name: 'whatever' }

You'll be then able to query the original position and filename where that object is declared.

1 Comment

This functionality exists in chrome i.e., I can get the filename and line number of declared function in javascript by just clicking show function code (devtools). So the information is kept somewhere in V8 but I am not able to get it (through remote debugging for example). Source map would be the perfect fit, however my approach is blackbox (I am not controlling the script I am loading)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.