Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is my script src URL?

Is there a simple and reliable way to determine the URL of the currently-executing JavaScript file (inside a web page)?

My only thought on this is to scan the DOM for all the script src attributes to find how the current file was referenced and then figure out the absolute URL by applying it to document.location. Anyone have other ideas, is there some super-easy method I completely overlooked?

UPDATE: Script elements accessed via the DOM already have a src property which contains the full URL. I don't know how ubiquitous/standard that is, but alternatively you can use getAttribute("src") which will return whatever raw attribute value is in the [X]HTML.

like image 554
Neil C. Obremski Avatar asked Jun 12 '09 00:06

Neil C. Obremski


People also ask

What is script URL?

The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again.

Where does script src go?

Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

How do you link a script in HTML?

We can link JavaScript to HTML by adding all the JavaScript code inside the HTML file. We achieve this using the script tag which was explained earlier. We can put the <script></script> tag either inside the head of the HTML or at the end of the body.


2 Answers

Put this in the js file that needs to know it's own url.

Fully Qualified (eg http://www.example.com/js/main.js):

var scriptSource = (function(scripts) {     var scripts = document.getElementsByTagName('script'),         script = scripts[scripts.length - 1];      if (script.getAttribute.length !== undefined) {         return script.src     }      return script.getAttribute('src', -1) }()); 

Or As it appears in source (eg /js/main.js):

var scriptSource = (function() {     var scripts = document.getElementsByTagName('script'),         script = scripts[scripts.length - 1];      if (script.getAttribute.length !== undefined) {         return script.getAttribute('src')     }      return script.getAttribute('src', 2) }()); 

See http://www.glennjones.net/Post/809/getAttributehrefbug.htm for explanation of the getAttribute parameter being used (it's an IE bug).

like image 174
Crescent Fresh Avatar answered Oct 08 '22 13:10

Crescent Fresh


For recent browsers, you can use document.currentScript to get this information.

var mySource = document.currentScript.src; 

The upside is that it's more reliable for scripts that are loaded asynchronously. The downside is that it's not, as best I know, universally supported. It should work on Chrome >= 29, FireFox >= 4, Opera >= 16. Like many useful things, it doesn't seem to work in IE.

When I need to get a script path, I check to see if document.currentScript is defined, and, if not, use the method described in the accepted answer.

if (document.currentScript) {     mySource = document.currentScript.src; } else {     // code omitted for brevity } 

https://developer.mozilla.org/en-US/docs/Web/API/document.currentScript

like image 30
kiprainey Avatar answered Oct 08 '22 14:10

kiprainey