Javascript 如何获取当前正在执行的javascript代码的文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2255689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:29:24  来源:igfitidea点击:

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

javascriptincludeabsolute-pathsrc

提问by B T

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?

我正在尝试使用 C#include "filename.c"或 PHP 之类的东西,include(dirname(__FILE__)."filename.php")但使用 javascript。我知道如果我可以获取加载 js 文件的 URL(例如标签的 src 属性中给出的 URL),我就可以做到这一点。javascript 有没有办法知道这一点?

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?

或者,有没有什么好方法可以从同一个域中动态加载 javascript(不具体知道域)?例如,假设我们有两个相同的服务器(QA 和生产),但它们显然具有不同的 URL 域。有没有办法做一些类似于include("myLib.js");myLib.js 将从加载它的文件域加载的事情?

Sorry if thats worded a little confusingly.

对不起,如果那措辞有点令人困惑。

回答by InfinitiesLoop

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 srcsomewhere where you can use it later. Avoid leaking global variables by wrapping it in:

这是有效的,因为浏览器按顺序加载和执行脚本,因此当您的脚本正在执行时,包含它的文档肯定会将您的脚本元素作为页面上的最后一个元素。这段代码当然必须是脚本的“全局”代码,因此请保存src在以后可以使用的地方。通过将其包装在以下位置避免泄漏全局变量:

(function() { ... })();

回答by Rudie

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

除了 Internet Explorer(任何版本)之外的所有浏览器都有document.currentScript,它始终有效(无论文件是如何包含的(异步、书签等))。

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

如果你想知道你现在所在的 JS 文件的完整 URL:

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

Tadaa.

多阿。

回答by Kevin Leary

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

如果您的文档中有内联脚本,则此处接受的答案不起作用。为避免这种情况,您可以使用以下内容仅定位<script>具有[src]属性的标签。

/**
 * 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 last external .js file, solving some issues I encountered with inline JS templates.

这有效地捕获了最后一个外部 .js 文件,解决了我在使用内联 JS 模板时遇到的一些问题。

回答by Alexandre Daubricourt

I just made this little trick :

我刚刚做了这个小技巧:

window.getRunningScript = () => {
    return () => {      
        return new Error().stack.match(/([^ \n])*([a-z]*:\/\/\/?)*?[a-z0-9\/\]*\.js/ig)[0]
    }
}
console.log('%c Currently running script:', 'color: blue', getRunningScript()())

screenshot

截屏

? Works on: Chrome, Firefox, Edge, Opera

? 适用于:Chrome、Firefox、Edge、Opera

Enjoy !

享受 !

回答by Stoutie

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

根据这里找到的答案,我想出了以下几点:

getCurrentScript.js

获取当前脚本.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

获取当前脚本路径.js

var getCurrentScript = require('./getCurrentScript');

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

module.exports = getCurrentScriptPath;

BTW: I'm using CommonJSmodule format and bundling with webpack.

顺便说一句:我正在使用CommonJS模块格式并与webpack捆绑。

回答by B T

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

我最近发现了一种更简洁的方法,可以随时执行,而不是在脚本加载时强制同步执行。

Use stackinfoto get a stacktrace at a current location, and grab the info.filename off the top of the stack.

使用stackinfo获取当前位置info.file的堆栈跟踪,并从堆栈顶部获取名称。

info = stackinfo()
console.log('This is the url of the script '+info[0].file)

回答by Afonso Matos

I've coded a simple function which allows to get the absolute location of the current javascript file, by using a try/catch method.

我编写了一个简单的函数,它允许使用 try/catch 方法获取当前 javascript 文件的绝对位置。

// Get script file location
// doesn't work for older browsers

var getScriptLocation = function() {
    var fileName    = "fileName";
    var stack       = "stack";
    var stackTrace  = "stacktrace";
    var loc     = null;

    var matcher = function(stack, matchedLoc) { return loc = matchedLoc; };

    try { 

        // Invalid code
        0();

    }  catch (ex) {

        if(fileName in ex) { // Firefox
            loc = ex[fileName];
        } else if(stackTrace in ex) { // Opera
            ex[stackTrace].replace(/called from line \d+, column \d+ in (.*):/gm, matcher);
        } else if(stack in ex) { // WebKit, Blink and IE10
            ex[stack].replace(/at.*?\(?(\S+):\d+:\d+\)?$/g, matcher);
        }

        return loc;
    }

};

You can see it here.

你可以在这里看到它。

回答by johnmdonahue

I may be misunderstanding your question but it seems you should just be able to use a relative path as long as the production and development servers use the same path structure.

我可能误解了您的问题,但似乎只要生产和开发服务器使用相同的路径结构,您就应该能够使用相对路径。

<script language="javascript" src="js/myLib.js" />

回答by Graza

Regardless of whether its a script, a html file (for a frame, for example), css file, image, whatever, if you dontspecify a server/domain the path of the html doc will be the default, so you could do, for example,

不管它是一个脚本、一个 html 文件(例如一个框架)、css 文件、图像等等,如果你指定服务器/域,html 文档的路径将是默认的,所以你可以这样做,例如,

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

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

or

或者

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

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

If you don't provide the server/domain, the path will be relative to either the path of the page or script of the main document's path

如果您不提供服务器/域,则路径将相对于页面路径或主文档路径的脚本

回答by Andrej

Refining upon the answers found here:

改进在这里找到的答案:

little trick

小把戏

getCurrentScript and getCurrentScriptPath

getCurrentScript 和 getCurrentScriptPath

I came up with the following:

我想出了以下内容:

//Thanks to https://stackoverflow.com/a/27369985/5175935
var getCurrentScript = function () {

    if ( document.currentScript && ( document.currentScript.src !== '' ) )
        return document.currentScript.src;
    var scripts = document.getElementsByTagName( 'script' ),
        str = scripts[scripts.length - 1].src;
    if ( str !== '' )
        return src;
    //Thanks to https://stackoverflow.com/a/42594856/5175935
    return new Error().stack.match(/(https?:[^:]*)/)[0];

};

//Thanks to https://stackoverflow.com/a/27369985/5175935
var getCurrentScriptPath = function () {
    var script = getCurrentScript(),
        path = script.substring( 0, script.lastIndexOf( '/' ) );
    return path;
};