4

Is any way to get the file info (file name, path) of a javascript file after the document is ready, without knowing how many scripts or the order how they are loaded?

i.e.

<html> <head> <script src="http://www.example.ex/js/js1.js" type="text/javascript"> <script src="http://www.example.ex/javascript/js2.js" type="text/javascript"> <script src="http://www.example.ex/scripts/js3.js" type="text/javascript"> <...> 

Where the file js2.js has something like this:

$(document).ready(function() { // get the filename "js2.js" } 

I could do

var scripts = document.getElementsByTagName("script"), scriptLocation = scripts[1].src; 

but the index "[1]" must be dynamic because i don't know the order of the loaded scripts.

4
  • 1
    possible duplicate of how to get the absolute path of the current javascript file name. As the accepted answer there says: "Of course this will only work at time of initial code run and would not be useful for example within a function that is called after initial script is loaded, so if you need the value available later, you would need to save it to a variable." Commented Sep 20, 2013 at 13:59
  • As that answer says, the currently-running script is always scripts[scripts.length - 1]. You just need to save a reference to the name before the document ready handler. Commented Sep 20, 2013 at 14:34
  • 1
    @MattBall I suppose you mean that a script is only executed once when it is loaded. Then is my mistake and i should have asked After the document is full loaded, when i call a function, how can i know the index of the script where this function is without saving it before? Hope you understand me. Commented Sep 20, 2013 at 14:45
  • Without saving it before, there isn't a 100% correct way to do it. The best you can do it use a stack trace hack. There's nothing to say that an arbitrary JS snippet executed at an arbitrary time even exists in a file. I'm curious to know why you want to do this at all, since this is really starting to smell like an XY problem to me. Commented Sep 20, 2013 at 14:56

5 Answers 5

3

To get Line Number and filename from Error.stack eg:

console.log((new Error).stack.split("\n")); 

See Error.stack

For browser compatibility, see previous SO question

Sign up to request clarification or add additional context in comments.

1 Comment

This is the best answer, though it's hacky and the browser support is spotty.
3

Perhaps a full answer would be more helpful than my comments. If you put this code into js2.js, you'll get what you want. The key is to capture scriptLocation in a piece of code which runs synchronously with the loading the file, which means not in a callback.

var scripts = document.getElementsByTagName("script"), scriptLocation = scripts[scripts.length - 1].src; $(document).ready(function() { // logs the full path corresponding to "js2.js" console.log(scriptLocation); } 

1 Comment

Exactly! This is what i mean and i was already using this solution. But i would like to get the same information without the 2 first lines, or i need to write this lines on every script and save the variable scriptLocation as an array.
1

Not entirely sure what you're after, but if you want to get reference to all the scripts loaded in the DOM, you would simply do:

var scripts = document.getElementsByTagName("script"); for(var i = 0; i < scripts.length; i++){ var scriptLocation = scripts[i].src; } 

2 Comments

This really does't answer the question.
I can't say I quite understand the question, but he seemed to infer he needed a dynamic index, which I provided. He should now have access to all the script locations after the document is loaded. If the OP needs something else, he is welcome to specify what.
0
var scripts = document.getElementsByTagName("script"); for (var i = 0; i < scripts.length; i++) { if (scripts[i].src.match(/<desired filename>/) { scriptLocation = scripts[i]; } } 

If I correctly understood your question, that should do the trick.

3 Comments

yep, but i don't know the /<desired filename>/. I only know it is the current executing file.
In that case, I'm not quite sure what you are trying to achieve.
My question starts with: Is any way to get the file info (file name, path). So i don't know the "desired file name".
0
var scriptName = [].slice.call(document.getElementsByTagName('script')).pop().getAttribute('src'); 

document.getElementsByTagName('script') return HTMLCollection of page scripts. Last element is current script. HTMLCollection doesn't have method to get last, but after convert colliection by [].slice.call to Array we can call pop to do it. Finally getAttribute return desired filename.

Same code can used to passing arguments to js-script

<script src="file.js" args="arg1;arg2"> ... var args = [].slice.call(document.getElementsByTagName('script')).pop().getAttribute('args').split(';'); 

2 Comments

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations!
:) Add explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.