[英]How can I get the current directory name in Javascript?
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?有任何想法吗?
window.location.pathname will get you the directory, as well as the page name. window.location.pathname 将为您提供目录以及页面名称。 You could then use .substring() to get the directory:
然后你可以使用 .substring() 来获取目录:
var loc = window.location.pathname;
var dir = loc.substring(0, loc.lastIndexOf('/'));
Hope this helps!希望这可以帮助!
在 Node.js 中,您可以使用:
console.log('Current directory: ' + process.cwd());
You can use window.location.pathname.split('/');
您可以使用
window.location.pathname.split('/');
That will produce an array with all of the items between the /'s这将产生一个数组,其中包含 / 之间的所有项目
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('/'));
complete URL完整网址
If you want the complete URL eg http://website/basedirectory/workingdirectory/
use:如果您想要完整的 URL,例如
http://website/basedirectory/workingdirectory/
使用:
var location = window.location.href;
var directoryPath = location.substring(0, location.lastIndexOf("/")+1);
local path本地路径
If you want the local path without domain eg /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 +1
after location.lastIndexOf("/")+1
.如果您不需要末尾的斜杠,请删除
location.lastIndexOf("/")+1
之后的+1
。
directory name目录名
If you only want the current directory name, where the script is running in, eg workingdirectory
use:如果您只想要脚本运行所在的当前目录名称,例如
workingdirectory
使用:
var location = window.location.pathname;
var path = location.substring(0, location.lastIndexOf("/"));
var directoryName = path.substring(path.lastIndexOf("/")+1);
For both / and \\:对于 / 和 \\:
window.location.pathname.replace(/[^\\\/]*$/, '');
To return without the trailing slash, do:要返回不带斜杠,请执行以下操作:
window.location.pathname.replace(/[\\\/][^\\\/]*$/, '');
这个单行作品:
var currentDirectory = window.location.pathname.split('/').slice(0, -1).join('/')
Assuming you are talking about the current URL, you can parse out part of the URL using window.location
.假设您正在谈论当前 URL,您可以使用
window.location
解析出部分 URL。 See: http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript请参阅: http : //java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript
An interesting approach to get the dirname
of the current URL is to make use of your browser's built-in path resolution.获取当前 URL
dirname
的一种有趣方法是利用浏览器的内置路径解析。 You can do that by:你可以这样做:
.
.
, ie the current directory HTMLAnchorElement
interface of the link to get the resolved URL or path equivalent to .
HTMLAnchorElement
接口获取解析的 URL 或等效于.
. Here's one line of code that does just that:这是执行此操作的一行代码:
Object.assign(document.createElement('a'), {href: '.'}).pathname
In contrast to some of the other solutions presented here, the result of this method will always have a trailing slash.与此处介绍的其他一些解决方案相比,此方法的结果将始终具有尾部斜杠。 Eg running it on this page will yield
/questions/3151436/
, running it on https://stackoverflow.com/
will yield /
.例如,在此页面上运行它会产生
/questions/3151436/
,在https://stackoverflow.com/
上运行它会产生/
。
It's also easy to get the full URL instead of the path.获取完整 URL 而不是路径也很容易。 Just read the
href
property instead of pathname
.只需阅读
href
属性而不是pathname
。
Finally, this approach should work in even the most ancient browsers if you don't use Object.assign
:最后,如果您不使用
Object.assign
,这种方法甚至应该适用于最古老的浏览器:
function getCurrentDir () {
var link = document.createElement('a');
link.href = '.';
return link.pathname;
}
window.location .pathname
如果你想要完整的 URL,例如website.com/workingdirectory/
使用: window.location.hostname+window.location.pathname.replace(/[^\\\\\\/]*$/, '');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.