【问题标题】:How can I get the current directory name in Javascript?如何在 Javascript 中获取当前目录名称?
【发布时间】:2010-06-30 16:43:45
【问题描述】:

我正在尝试在 Javascript 中获取文件的当前目录,以便我可以使用它来为我网站的每个部分触发不同的 jquery 函数。

if (current_directory) = "example" {
var activeicon = ".icon_one span";
};
elseif (current_directory) = "example2" {
var activeicon = ".icon_two span";
};
else {
var activeicon = ".icon_default span";
};

$(activeicon).show();
...

有什么想法吗?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    在 Node.js 中,您可以使用:

    console.log('Current directory: ' + process.cwd());
    

    【讨论】:

    • 请描述更多。
    • nodejs 平台的这个答案。在回答这个问题之前,你应该告诉你在哪里运行 javascript。
    • 例如。在主 server.js 文件中作为常量在任何情况下...
    【解决方案2】:

    window.location.pathname 将为您提供目录以及页面名称。然后您可以使用 .substring() 来获取目录:

    var loc = window.location.pathname;
    var dir = loc.substring(0, loc.lastIndexOf('/'));
    

    希望这会有所帮助!

    【讨论】:

    【解决方案3】:

    完整网址

    如果您想要完整的 URL,例如http://website/basedirectory/workingdirectory/使用:

    var location = window.location.href;
    var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
    

    本地路径

    如果您想要没有域的本地路径,例如/basedirectory/workingdirectory/使用:

    var location = window.location.pathname;
    var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
    

    如果您不需要末尾的斜线,请删除location.lastIndexOf("/")+1 之后的+1

    目录名称

    如果您只想要当前目录名称,即脚本运行的位置,例如workingdirectory使用:

    var location = window.location.pathname;
    var path = location.substring(0, location.lastIndexOf("/"));
    var directoryName = path.substring(path.lastIndexOf("/")+1);
    

    【讨论】:

    • location 导致谷歌浏览器重新加载页面,将变量名称更改为 location1 之类的其他名称
    • 这为我节省了很多时间!谢谢
    • 你是我今天的天使!
    【解决方案4】:

    如果您不是在谈论 URL 字符串,这将适用于文件系统上的实际路径。

    var path = document.location.pathname;
    var directory = path.substring(path.indexOf('/'), path.lastIndexOf('/'));
    

    【讨论】:

    • var path = document.location.pathname; var dir = path.substring(path.indexOf('/'), path.lastIndexOf('/'));
    【解决方案5】:

    您可以使用window.location.pathname.split('/');

    这将产生一个数组,其中包含 / 之间的所有项目

    【讨论】:

      【解决方案6】:

      这种单线工作:

      var currentDirectory = window.location.pathname.split('/').slice(0, -1).join('/')
      

      【讨论】:

      • 试图取消我的反对票,但 SO 不会让我这样做,因为我最近对此进行了投票。这很有意义......
      【解决方案7】:

      获取当前 URL 的dirname 的一种有趣方法是利用浏览器的内置路径解析。你可以这样做:

      1. 创建指向.的链接,即当前目录
      2. 使用链接的HTMLAnchorElement接口获取解析后的URL或等效于.的路径。

      这里有一行代码可以做到这一点:

      Object.assign(document.createElement('a'), {href: '.'}).pathname
      

      与此处介绍的其他一些解决方案相比,此方法的结果将始终有一个尾部斜杠。例如。在这个页面上运行它会产生/questions/3151436/,在https://stackoverflow.com/上运行它会产生/

      获取完整 URL 而不是路径也很容易。只需阅读 href 属性而不是 pathname

      最后,如果您不使用Object.assign,这种方法甚至可以在最古老的浏览器中使用:

      function getCurrentDir () {
          var link = document.createElement('a');
          link.href = '.';
          return link.pathname;
      }
      

      【讨论】:

        【解决方案8】:

        如果您想要完整的 URL,例如website.com/workingdirectory/ 使用: window.location.hostname+window.location.pathname.replace(/[^\\\/]*$/, '');

        【讨论】:

          【解决方案9】:

          window.location.pathname

          【讨论】:

            【解决方案10】:

            对于 / 和 \:

            window.location.pathname.replace(/[^\\\/]*$/, '');
            

            要返回不带斜杠,请执行以下操作:

            window.location.pathname.replace(/[\\\/][^\\\/]*$/, '');
            

            【讨论】:

            • 这适用于 IE
            【解决方案11】:

            假设您正在谈论当前 URL,您可以使用 window.location 解析出部分 URL。 见:http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript

            【讨论】:

              猜你喜欢
              • 2020-07-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-04-17
              • 2017-09-05
              • 1970-01-01
              • 2010-11-18
              • 2016-04-05
              相关资源
              最近更新 更多