Manohar
Manohar

Reputation: 1330

Can javascript file get its own name?

I am trying to build some sort of logger functionality in javascript. Is there any API for a script to get its own filename?

Upvotes: 11

Views: 15700

Answers (7)

Bryan
Bryan

Reputation: 17601

This works for me.

document.currentScript.src

Upvotes: 0

johnnydanger
johnnydanger

Reputation: 49

You can try putting this at the top of your JavaScript file:

window.myJSFilename = "";
window.onerror = function(message, url, line) {
    if (window.myJSFilename != "") return;
    window.myJSFilename =  url;
}
throw 1;

Make sure you have only functions below this. The myJSFilename global variable will contain the full path of the JavaScript file, and the filename can be parsed from that. Tested in IE11, but it should work elsewhere.

Upvotes: 0

Sheffie
Sheffie

Reputation: 335

If we can get the current script's tag, then we can read its src attribute. Excerpt from https://stackoverflow.com/a/22745553/4396817 below:

document.currentScript will return the element whose script is currently being processed.

<script>
  var me = document.currentScript;
</script>

Benefits

  • Simple and explicit. Reliable.
  • Don't need to modify the script tag
  • Works with asynchronous scripts (defer & async)
  • Works with scripts inserted dynamically

Problems

  • Will not work in older browsers and IE.

...So from there, we can simply read the src attribute!

<script src="http://website.com/js/script.js">
  alert(document.currentScript.src);
</script>

// Alerts "http://website.com/js/script.js"

Upvotes: 9

Smellymoo
Smellymoo

Reputation: 97

this is my modification that fixes a few possible issues, but adds a requirement.

It needs you to name the file in a certain way, so for example if you have a .js file, but you want to know which version is loaded (for example to tell a php server). so your js file would be "zoom_v34.js".

    var version;
    (function(){
        var scripts = document.getElementsByTagName('script');
        for (var i=0; i<scripts.length; i++) {
            var start = scripts[i].src.indexOf('zoom_');
            if (start != -1) { var end = scripts[i].src.indexOf('.',start); version = scripts[i].src.substr(start+6,end-start-6); break; }
        }
    }());
    post='login{JS:'+version+'}';

Upvotes: 0

pawel
pawel

Reputation: 36995

This should work: (new Error).fileName

Or you can try this:

var filepath;
(function(){ 
    var scripts = document.getElementsByTagName('script'); 
    filepath = scripts[ scripts.length-1 ].src; 
}());

The second option gives you the path of your script file.

Upvotes: 19

dereli
dereli

Reputation: 1864

Unfortunately this is not possible.

If you change your approach, getting function names may help you which is sometimes possible. Your best chance would be extracting function name from "arguments.callee". This only works if function is defined like

function FN() { ... }

And does not work when

var FN = function() { ... }

Upvotes: 0

hsz
hsz

Reputation: 152294

I see two ways:

  • put into every JS file a variable var filename = 'script.js';
  • get the filename using <script> tag name

JS can not get filename like bash/perl/c scripts.

Upvotes: 6

Related Questions