如何在Javascript中获取当前目录名?

时间:2022-05-29 23:20:57

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?

什么好主意吗?

9 个解决方案

#1


66  

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。路径名将获取目录和页面名。然后可以使用.substring()获取目录:

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

Hope this helps!

希望这可以帮助!

#2


19  

You can use window.location.pathname.split('/');

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

That will produce an array with all of the items between the /'s

这会产生一个包含/ s之间所有项的数组

#3


9  

For both / and \:

For both / and \:

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

To return without the trailing slash, do:

要返回没有结尾的斜杠,请:

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

#4


8  

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('/'));

#5


4  

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,您可以使用windows .location解析URL的一部分。参见:http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript

#6


3  

This one-liner works:

这一行程序工作原理:

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

#7


2  

If you want the complete URL e.g. http://website/basedirectory/workingdirectory/ use:

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

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/ use:

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。

If you only want the current directory name, where the script is running in, e.g. workingdirectory use:

如果您只想要当前的目录名,其中脚本正在运行,例如:workingdirectory使用:

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

#8


1  

window.location.pathname

window.location.pathname

#9


1  

In Node.js, you could use:

在节点。js,您可以使用:

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

#1


66  

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。路径名将获取目录和页面名。然后可以使用.substring()获取目录:

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

Hope this helps!

希望这可以帮助!

#2


19  

You can use window.location.pathname.split('/');

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

That will produce an array with all of the items between the /'s

这会产生一个包含/ s之间所有项的数组

#3


9  

For both / and \:

For both / and \:

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

To return without the trailing slash, do:

要返回没有结尾的斜杠,请:

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

#4


8  

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('/'));

#5


4  

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,您可以使用windows .location解析URL的一部分。参见:http://java-programming.suite101.com/article.cfm/how_to_get_url_parts_in_javascript

#6


3  

This one-liner works:

这一行程序工作原理:

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

#7


2  

If you want the complete URL e.g. http://website/basedirectory/workingdirectory/ use:

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

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/ use:

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。

If you only want the current directory name, where the script is running in, e.g. workingdirectory use:

如果您只想要当前的目录名,其中脚本正在运行,例如:workingdirectory使用:

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

#8


1  

window.location.pathname

window.location.pathname

#9


1  

In Node.js, you could use:

在节点。js,您可以使用:

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