如何在Javascript中获取当前目录名称? [英] How can I get the current directory name in Javascript?

查看:75
本文介绍了如何在Javascript中获取当前目录名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get the current directory of the file in Javascript so I can use that to trigger a different jquery function for each section of my site.

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();
...

任何想法?

推荐答案

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

window.location.pathname will get you the directory, as well as the page name. You could then use .substring() to get the directory:

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

希望这有帮助!

这篇关于如何在Javascript中获取当前目录名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆