如何在 Javascript 中获取当前目录名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3151436/
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
How can I get the current directory name in Javascript?
提问by user380303
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.
我正在尝试在 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();
...
Any ideas?
有任何想法吗?
回答by Ryan Kinal
window.location.pathname will get you the directory, as well as the page name. You could then use .substring() to get the directory:
window.location.pathname 将为您提供目录以及页面名称。然后你可以使用 .substring() 来获取目录:
var loc = window.location.pathname;
var dir = loc.substring(0, loc.lastIndexOf('/'));
Hope this helps!
希望这可以帮助!
回答by philip brodovsky
In Node.js, you could use:
在 Node.js 中,您可以使用:
console.log('Current directory: ' + process.cwd());
回答by Rob
You can use window.location.pathname.split('/');
您可以使用 window.location.pathname.split('/');
That will produce an array with all of the items between the /'s
这将产生一个数组,其中包含 / 之间的所有项目
回答by bpeterson76
This will work for actual paths on the file system if you're not talking the URL string.
如果您不是在谈论 URL 字符串,这将适用于文件系统上的实际路径。
var path = document.location.pathname;
var directory = path.substring(path.indexOf('/'), path.lastIndexOf('/'));
回答by David Sherret
For both / and \:
对于 / 和 \:
window.location.pathname.replace(/[^\\/]*$/, '');
To return without the trailing slash, do:
要返回不带斜杠,请执行以下操作:
window.location.pathname.replace(/[\\/][^\\/]*$/, '');
回答by flavio.donze
If you want the complete URL e.g. http://website/basedirectory/workingdirectory/use:
如果您想要完整的 URL,例如http://website/basedirectory/workingdirectory/使用:
var location = window.location.href;
var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
If you want the local path without domain e.g. /basedirectory/workingdirectory/use:
如果您想要没有域的本地路径,例如/basedirectory/workingdirectory/使用:
var location = window.location.pathname;
var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
In case you don't need the slash at the end, remove the +1after location.lastIndexOf("/")+1.
如果您不需要末尾的斜杠,请删除+1after location.lastIndexOf("/")+1。
If you only want the current directory name, where the script is running in, e.g. workingdirectoryuse:
如果您只需要当前目录名称,即脚本运行所在的目录,例如workingdirectory使用:
var location = window.location.pathname;
var path = location.substring(0, location.lastIndexOf("/"));
var directoryName = path.substring(path.lastIndexOf("/")+1);
回答by jbyrd
This one-liner works:
这个单行作品:
var currentDirectory = window.location.pathname.split('/').slice(0, -1).join('/')
回答by David Radcliffe
Assuming you are talking about the current URL, you can parse out part of the URL using window.location.
See: http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript
假设您正在谈论当前的 URL,您可以使用window.location. 请参阅:http: //java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript
回答by Sky Sanders
window.location.pathname
window.location的.pathname

