How to get the path to javascript executable code file

I am trying to do something like 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 downloaded from the js file (for example, the URL specified in the src attribute of the tag). Is there any way for javascript to know this?

Alternatively, is there a good way to load javascript from a dynamic domain from one domain (without knowing the domain specifically)? For example, let's say we have two identical servers (QA and production), but they obviously have different URLs. Is there a way to do something like include("myLib.js"); where will myLib.js be loaded from the domain of the file loading it?

Sorry if this is a little puzzled.

+42
javascript include absolute-path src
Feb 12 '10 at 23:00
source share
9 answers

Inside the script:

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

This works because the browser loads and runs the scripts in order, so while your script is executing, the document in which it was included will necessarily contain your script element as the last on the page. This code, of course, should be β€œglobal” for the script, so save the 'src' somewhere where you can use it later. Avoid leakage of global variables by transferring them to:

 (function() { ... })(); 
+66
Feb 12 '10 at 23:08
source share

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 find out the full URL of the JS file you are currently at:

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

Tadaa.

+23
Oct 25 '13 at 12:09 on
source share

The accepted answer here does not work if you have embedded scripts in your document. To avoid this, you can use the following to use only <script> tags with the [src] attribute.

 /** * Current Script Path * * Get the dir path to the currently executing script file * which is always the last one in the scripts array with * an [src] attr */ var currentScriptPath = function () { var scripts = document.querySelectorAll( 'script[src]' ); var currentScript = scripts[ scripts.length - 1 ].src; var currentScriptChunks = currentScript.split( '/' ); var currentScriptFile = currentScriptChunks[ currentScriptChunks.length - 1 ]; return currentScript.replace( currentScriptFile, '' ); } 

This effectively captures the latest external .js file, solving some of the problems I encountered with the built-in JS templates.

+12
Sep 24 '14 at 17:40
source share

I recently found a much cleaner approach to this that can be executed at any time, rather than being forced to do this synchronously when the script loads.

Use stackinfo to get stacktrace at its current location and grab the name info.file at the top of the stack.

 info = stackinfo() console.log('This is the url of the script '+info[0].file) 
+3
May 05 '14 at 12:28
source share

Refining the answers found here, I came up with the following:

getCurrentScript.js

 var getCurrentScript = function () { if (document.currentScript) { return document.currentScript.src; } else { var scripts = document.getElementsByTagName('script'); return scripts[scripts.length-1].src; } }; module.exports = getCurrentScript; 

getCurrentScriptPath.js

 var getCurrentScript = require('./getCurrentScript'); var getCurrentScriptPath = function () { var script = getCurrentScript(); var path = script.substring(0, script.lastIndexOf('/')); return path; }; module.exports = getCurrentScriptPath; 

By the way: I am using the CommonJS module and binding with webpack .

+3
Dec 09 '14 at 1:25
source share

I have encoded a simple function that allows you to get the absolute location of the current javascript file using the try / catch method.

You can see it here .

+2
Oct 11 '14 at 21:35
source share

I just did this little trick:

 window.getRunningScript = () => { return () => { let err = new Error() let link = err.stack.split('(') link = link[1] link = link.split(')')[0] link = link.split(':') link.splice(-2, 2) link = link.join(':') return link } } console.log('%c Currently running script:', 'color: blue', getRunningScript()()) 

screenshot

Work on: Chrome, Firefox, Edge

enjoy it!

+2
Mar 04 '17 at 10:23
source share

I may not understand your question, but it seems that you should just use the relative path if the production and development servers use the same path structure.

 <script language = "javascript" src = "js / myLib.js" />
+1
Feb 12 2018-10-12T00
source share

Regardless of whether it is a script, an html file (for example, for a frame), a css file, an image, any, if you do not specify a server / domain, the path to the html document will be the default, so you can do, for example,

<script type=text/javascript src='/dir/jsfile.js'></script>

or

<script type=text/javascript src='../../scripts/jsfile.js'></script>

If you do not provide a server / domain, the path will refer to either the page path or the script of the main document path

0
Feb 12 2018-10-12T00
source share



All Articles