JavaScript - How to get the url of the script that is being called?

I am including myscript.js in the http://site1.com/index.html file as follows:

 <script src=http://site2.com/myscript.js></script> 

Inside "myscript.js" I want to access the URL " http://site2.com/myscript.js ". I would like to have something like this:

 function getScriptURL() { // something here return s } alert(getScriptURL()); 

To warn " http://site2.com/myscript.js " if called from the index.html above.

+62
javascript
Jun 04 '10 at 18:13
source share
9 answers

From http://feather.elektrum.org/book/src.html :

 var scripts = document.getElementsByTagName('script'); var index = scripts.length - 1; var myScript = scripts[index]; 

The myScript variable myScript has a script dom element. You can get url src using myScript.src .

Note that this must be done as part of the initial evaluation script. If you want to not pollute the Javascript namespace, you can do something like:

 var getScriptURL = (function() { var scripts = document.getElementsByTagName('script'); var index = scripts.length - 1; var myScript = scripts[index]; return function() { return myScript.src; }; })(); 
+50
Jun 04 '10 at 18:22
source share

You can add the id attribute to your script tag (even if it is inside the main tag):

 <script id="myscripttag" src="http://site2.com/myscript.js"></script> 

and then access its src as follows:

 document.getElementById("myscripttag").src 

Of course, the id value should be the same for every document that your script includes, but I don't think this is a big inconvenience for you.

+40
Aug 08 2018-11-12T00:
source share

Using the DOM and querySelector you can take for a specific script:

 var dir = document.querySelector('script[src$="myscript.js"]').getAttribute('src'); var name = dir.split('/').pop(); dir = dir.replace('/'+name,""); 
+23
Dec 29 '13 at 10:02
source share

I wrote a class to find a script path that works with load delay and async script tags.

I had some template files that were relative to my scripts, so instead of hard coding, I created a created class to create paths automatically. The full source is here on github.

Some time ago I had arguments.callee arguments to try to do something similar, but I recently read in MDN that it is not allowed in strict mode .

 function ScriptPath() { var scriptPath = ''; try { //Throw an error to generate a stack trace throw new Error(); } catch(e) { //Split the stack trace into each line var stackLines = e.stack.split('\n'); var callerIndex = 0; //Now walk though each line until we find a path reference for(var i in stackLines){ if(!stackLines[i].match(/http[s]?:\/\//)) continue; //We skipped all the lines with out an http so we now have a script reference //This one is the class constructor, the next is the getScriptPath() call //The one after that is the user code requesting the path info (so offset by 2) callerIndex = Number(i) + 2; break; } //Now parse the string for each section we want to return pathParts = stackLines[callerIndex].match(/((http[s]?:\/\/.+\/)([^\/]+\.js)):/); } this.fullPath = function() { return pathParts[1]; }; this.path = function() { return pathParts[2]; }; this.file = function() { return pathParts[3]; }; this.fileNoExt = function() { var parts = this.file().split('.'); parts.length = parts.length != 1 ? parts.length - 1 : 1; return parts.join('.'); }; } 
+17
Mar 04 '14 at 7:10
source share

Everything except IE supports

 document.currentScript 
+14
Aug 24 '16 at 13:05
source share

If you have the option to use jQuery, the code will look like this:

 $('script[src$="/myscript.js"]').attr('src'); 
+12
Aug 14 '13 at 3:03 on
source share

A simple and straightforward solution that works very well:

If it is not IE, you can use document.currentScript
For IE you can do document.querySelector('script[src*="myscript.js"]')
So:

 function getScriptURL(){ var script = document.currentScript || document.querySelector('script[src*="myscript.js"]') return script.src } 
+4
Jul 09 '17 at 15:06
source share

The following code allows you to find the script element with the given name

 var scripts = document.getElementsByTagName( 'script' ); var len = scripts.length for(var i =0; i < len; i++) { if(scripts[i].src.search("<your JS file name") > 0 && scripts[i].src.lastIndexOf("/") >= 0) { absoluteAddr = scripts[i].src.substring(0, scripts[i].src.lastIndexOf("/") + 1); break; } } 
+2
Apr 02 '14 at 5:54 on
source share

Can't you use location.href or location.host and then add the script name?

-6
Jun. 04 '10 at 18:16
source share



All Articles