Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the file-path of the currently executing javascript code

I'm trying to do something like a C #include "filename.c", or PHP include(dirname(__FILE__)."filename.php") but in javascript. I know I can do this if I can get the URL a js file was loaded from (e.g. the URL given in the src attribute of the tag). Is there any way for the javascript to know that?

Alternatively, is there any good way to load javascript dynamically from the same domain (without knowing the domain specifically)? For example, lets say we have two identical servers (QA and production) but they clearly have different URL domains. Is there a way to do something like include("myLib.js"); where myLib.js will load from the domain of the file loading it?

Sorry if thats worded a little confusingly.

like image 431
B T Avatar asked Feb 12 '10 23:02

B T


People also ask

How do I print the path of a file in JavaScript?

We can get the path of the present script in node. js by using __dirname and __filename module scope variables. __dirname: It returns the directory name of the current module in which the current script is located. __filename: It returns the file name of the current module.

Where is JavaScript file stored?

JavaScript files are stored on the server and sent to the browser. Only the browser does something with the code. You cannot do something on the server side with JavaScript if you don't have a back end. Save this answer.

How do I view a .JS file?

Open Notepad or TextEdit, open the template folder, then drag the . js file into Notepad or TextEdit and drop it. Open Notepad or TextEdit, select "file" then "open", browse to the template folder, select "all file types" and open the . js file that way.

How is a JavaScript file executed?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.


2 Answers

Within the script:

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

This works because the browser loads and executes scripts in order, so while your script is executing, the document it was included in is sure to have your script element as the last one on the page. This code of course must be 'global' to the script, so save src somewhere where you can use it later. Avoid leaking global variables by wrapping it in:

(function() { ... })(); 
like image 193
InfinitiesLoop Avatar answered Oct 04 '22 06:10

InfinitiesLoop


All browsers except Internet Explorer (any version) have document.currentScript, which always works always (no matter how the file was included (async, bookmarklet etc)).

If you want to know the full URL of the JS file you're in right now:

var script = document.currentScript; var fullUrl = script.src; 

Tadaa.

like image 29
Rudie Avatar answered Oct 04 '22 04:10

Rudie