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.

Miawa
  • 282
  • 1
  • 4
  • 10
  • 1
    possible duplicate of [how to get the absolute path of the current javascript file name](http://stackoverflow.com/questions/13261970/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."_ – Matt Ball Sep 20 '13 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. – Matt Ball Sep 20 '13 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. – Miawa Sep 20 '13 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](http://stackoverflow.com/a/18918729/139010). 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.](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Matt Ball Sep 20 '13 at 14:56

5 Answers5

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);
}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 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. – Miawa Sep 20 '13 at 14:55
2

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

Community
  • 1
  • 1
Jakub Kotrs
  • 5,823
  • 1
  • 14
  • 30
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;
}
Mister Epic
  • 16,295
  • 13
  • 76
  • 147
  • 1
    This really does't answer the question. – Matt Ball Sep 20 '13 at 14:01
  • 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. – Mister Epic Sep 20 '13 at 14:06
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.

Vince
  • 1,517
  • 2
  • 18
  • 43
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(';');
Aikon Mogwai
  • 4,954
  • 2
  • 18
  • 31
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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! – Blue Jul 22 '16 at 02:29
  • :) Add explanation. – Aikon Mogwai Jul 22 '16 at 08:44