【问题标题】:How to get the absolute path of the current javascript file name如何获取当前javascript文件名的绝对路径
【发布时间】:2012-11-07 01:23:25
【问题描述】:

我有一个名为 main.js 的 javascript 文件。

main.js里面我想得到这个当前文件的绝对路径,比如:

http://server/app/main.js

http://server/app/script/main.js

获取绝对路径最快的方法是什么?

【问题讨论】:

标签: javascript


【解决方案1】:

您可以在以下位置调查脚本收集:

var scripts = document.getElementsByTagName("script");

对于返回的scripts 数组中的每个元素,您可以访问其src 属性。

当前执行的包含文件将始终是scripts 数组中的最后一个。所以你可以通过scripts[scripts.length-1]访问它。

当然,这只会在初始代码运行时起作用,例如在加载初始脚本后调用的函数中没有用处,因此如果您以后需要可用的值,则需要将其保存到一个变量。

【讨论】:

  • 如果我们包含多个文件,它会起作用吗...那么每个js文件将如何显示它自己的src路径..??
  • 值得注意的是,您可能希望使用另一种方法从scripts 数组中获取所需的脚本。我使用这种方法,但有报告称,在 Firefox (blegh...) 中经常有另一个脚本在我的逻辑抓取该元素数组时被加载。明智的做法是抓取最后 3 个左右的文件并循环遍历它们,然后找到具有您要查找的文件名的文件。
【解决方案2】:

获取 javascript 文件的当前路径名

把它放在 /tmp 下的 apache 目录中,并命名为 test.html。访问网址

localhost/grader/test.html?blah=2#foobar

Javascript:

<html>
<script>
  alert(location.pathname);  // /tmp/test.html
  alert(location.hostname);  // localhost
  alert(location.search);    // ?blah=2
  alert(document.URL);       // http://localhost/tmp/test.html?blah=2#foobar
  alert(location.href);      // http://localhost/tmp/test.html?blah=2#foobar
  alert(location.protocol);  // http:
  alert(location.host);      // localhost
  alert(location.origin);    // http://localhost
  alert(location.hash);      // #foobar
</script>                            
</html>

关于位置属性的更多信息:http://www.w3schools.com/jsref/obj_location.asp

或者如果你有 jquery:

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
</script>
<script>
  $(location).attr('href');      // http://localhost/tmp/test.html?blah=2#foobar
  $(location).attr('pathname');  // /tmp/test.html
</script>
</html>

【讨论】:

    【解决方案3】:

    这会奏效。但是,它要求您已经知道脚本的文件名是什么。但在大多数情况下,您会知道这一点。

    function absFileLoc(filename) {
      var scriptElements = document.getElementsByTagName('script');
      for (var i = 0; i < scriptElements.length; i++) {
        var source = scriptElements[i].src;
        if (source.indexOf(filename) > -1) {
          var location = source.substring(0, source.indexOf(filename)) + filename;
          return location;
        }
      }
      return false;
    }
    

    【讨论】:

    【解决方案4】:

    现代方式

    获取绝对路径最快的方法是使用document.currentScript

    const url = document.currentScript.src;
    

    还可以使用URL 来读取 url 的组成部分:)

    const url = new URL('http://www.example.com/cats');
    console.log(url.hostname); // "www.example.com"
    console.log(url.pathname); // "/cats"
    

    所有现代浏览器都应该支持这些方法。

    【讨论】:

      【解决方案5】:
      var path = document.location.pathname
      

      将提供当前的html页面。

      如果您要查找当前脚本,请尝试本网站中提到的方式:

      http://bencarpenter.co.uk/javascript-path-to-the-current-script

      【讨论】:

        【解决方案6】:

        我知道这是一个较老的问题,但仍然是实际的,因为 IE 仍然没有提供脚本 src。

        我最好的形式是给脚本标签一个唯一的ID:

            <script id="kuvaScript" src="jscripts/kuva/kuva.min.js"></script>
        

        在我使用的脚本内部:

            var thisScript = $("#kuvaScript").attr("src");
            var thisPath = thisScript.split('/').slice(0, -1).join('/')+'/'; 
        

        它将保持 src 格式原样(相对或完整),并在 IE 和 Chrome 中进行了测试。

        希望它能为您节省很多时间。

        【讨论】:

          【解决方案7】:

          非常简单

          把这个放到你要获取路径的*.js文件中:

          let scripts = document.getElementsByTagName('script')
          let lastScript = scripts[scripts.length -1]
          console.log('lastScript', lastScript.src)
          

          【讨论】:

            【解决方案8】:

            如果你想获取当前文件名或目录,试试这个

            location.href.split('/')[ location.href.split('/').length - 1 ];

            【讨论】:

            • location.href 获取当前网页的url,而不是当前正在执行的javascript文件。
            • 我们首先获取 url,然后将其拆分以获取所需的部分。 location.href.split('/')[0]; //获取第一部分 location.href.split('/')[ location.href.split('/').length - 1 ]; //获取文件名
            • 不,这是不正确的。您正在获取页面的 url,而不是当前的 javascript 文件。如果您将该 javascript 代码放入包含在 www.def.com/page.html 中的文件 www.abc.com/file.js 中,则 location.href 将返回 www.def.com/page.html。问题是询问如何获取 javascript 文件的名称,即 www.abc.com/file.js。
            • 您是否在控制台窗口中尝试过我的代码?对我来说效果很好!
            • 是的,我试过了,它给出了 .html 而不是 .js 的位置。试试你自己,你会看到!
            猜你喜欢
            • 2010-09-22
            • 1970-01-01
            • 1970-01-01
            • 2019-08-31
            • 2012-01-21
            • 2013-11-17
            • 2012-04-24
            相关资源
            最近更新 更多