How can I get the script file name from inside this script?

I am sure the answer is no, but I thought I would ask anyway.

If my site refers to a script called "whatever.js", is it possible to get "whatever.js" from within this script? How:

var scriptName = ??? if (typeof jQuery !== "function") { throw new Error( "jQuery script needs to be loaded before " + scriptName + ". Check the <script> tag order."); } 

There are probably more problems than it costs to check for dependencies, but what the hell.

+25
javascript html
Apr 02 '09 at 18:16
source share
10 answers
 var scripts = document.getElementsByTagName('script'); var lastScript = scripts[scripts.length-1]; var scriptName = lastScript.src; alert("loading: " + scriptName); 

Tested in: FF 3.0.8, Chrome 1.0.154.53, IE6




See also: How can I reference the script tag that was loading the currently executable script?

+29
Apr 02 '09 at 18:25
source share

I know this is old, but I developed a better solution because all of the above does not work for Async scripts. With some tweaking, the following script may cover almost all use cases. Heres what worked for me:

 function getScriptName() { var error = new Error() , source , lastStackFrameRegex = new RegExp(/.+\/(.*?):\d+(:\d+)*$/) , currentStackFrameRegex = new RegExp(/getScriptName \(.+\/(.*):\d+:\d+\)/); if((source = lastStackFrameRegex.exec(error.stack.trim())) && source[1] != "") return source[1]; else if((source = currentStackFrameRegex.exec(error.stack.trim()))) return source[1]; else if(error.fileName != undefined) return error.fileName; } 

Not sure of support in Internet Explorer, but works fine in every other browser I tested on.

+15
Nov 06 '13 at 8:44
source share

You can use...

 var scripts = document.getElementsByTagName("script"), currentScriptUrl = (document.currentScript || scripts[scripts.length - 1]).src; 

currentScript() supported by all browsers except IE.

Make sure it is launched when the file is parsed and executed, and not in DOM or window ready mode.

If this is an empty string, your script block does not have or an empty src attribute.

+10
May 18 '12 at 16:31
source share

You can return a list of script elements on the page:

 var scripts = document.getElementsByTagName("script"); 

And then evaluate each of them and return its location:

 var location; for(var i=0; i<scripts.length;++i) { location = scripts[i].src; //Do stuff with the script location here } 
+3
Apr 02 '09 at 18:27
source share

I will leave it here just in case:

 var abc = __filename.split(__dirname+"/").pop(); 
+3
Apr 24 '17 at 11:16
source share

Shog9 offer shorter:

 alert("loading: " + document.scripts[document.scripts.length-1].src); 
+2
08 Sep '13 at
source share

Since the src attribute contains the full path to the script file, you can add a substring call to get only the file name.

 var path = document.scripts[document.scripts.length-1].src; var fileName = path.substring(path.lastIndexOf('/')+1); 
+2
Oct 30 '15 at 12:23
source share

I am having problems with the above code when retrieving the script name when the calling code is included in the .html file. Therefore, I developed this solution:

 var scripts = document.getElementsByTagName( "script" ) ; var currentScriptUrl = ( document.currentScript || scripts[scripts.length - 1] ).src ; var scriptName = currentScriptUrl.length > 0 ? currentScriptUrl : scripts[scripts.length-1].baseURI.split( "/" ).pop() ; 
+1
Nov 28 '15 at 15:42
source share

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 only have features below this. The myJSFilename variable will contain the full path to the JavaScript file, the file name can be parsed from this. Tested in IE11, but it should work elsewhere.

+1
Nov 08 '16 at 16:33
source share

What happens if the jQuery script does not exist? Are you going to display a message? I think this is a little better for debugging if something goes wrong, but it is not very useful for users.

I would say that just create pages in such a way that this incident does not happen, and in the rare case this will happen, just release the script.

0
Apr 03 '09 at 10:38
source share



All Articles