当只知道文件夹路径时,使用javascript/jquery从url获取当前文件名

时间:2020-12-07 16:01:59

I am trying to get the current filename from the url using:

我正在尝试从url获取当前文件名,使用:

$currentFile = window.location.pathname.split("/").pop();

This works fine if the full path is something like:

如果完整路径是:

http://www.yoursite.com/folder/index.php

It will return index.php, index.cfm, index.html etc.

它将返回索引。php,指数。cfm,指数。html等。

But when the url is http://www.yoursite.com/folder/

但是当url是http://www.yoursite.com/folder/。

I cannot retrieve the current filename, is this possible via js or jquery?

我无法检索当前文件名,是否可以通过js或jquery获取?

1 个解决方案

#1


5  

If you only have the path in the URL, then you cannot get the filename from it - not using jQuery, not using any other client-side method. This is because only the server that sends this file knows what this file is. Specifically, in the web server configuration, there's a directive, which indicates what filename to search for if only the directory name is specified. For example, in apache this can be

如果URL中只有路径,则无法从URL中获取文件名——不使用jQuery,不使用任何其他客户端方法。这是因为只有发送这个文件的服务器才知道这个文件是什么。具体地说,在web服务器配置中,有一个指令,指示如果只指定目录名,搜索什么文件名。例如,在apache中,这可以是

DirectoryIndex index.html index.php home.htm

This tells the server that for requests with only a directory name the server will attempt to serve file index.html from that directory; if it doesn't exist, then index.php; if that also doesn't exist then home.htm. If that one also doesn't exist, then the behaviour depends on other configuration options. Other web server software has similar configuration options.

这告诉服务器,对于只有目录名的请求,服务器将尝试为文件索引提供服务。从html目录;如果它不存在,那么index.php;如果那也不存在,那就回家。如果那个也不存在,那么行为取决于其他配置选项。其他web服务器软件也有类似的配置选项。

Hence, when you send a request like http://www.yoursite.com/folder/ to a server, only that server will know what file is actually used.

因此,当您向服务器发送类似http://www.yoursite.com/folder/的请求时,只有该服务器才知道实际使用的文件。

#1


5  

If you only have the path in the URL, then you cannot get the filename from it - not using jQuery, not using any other client-side method. This is because only the server that sends this file knows what this file is. Specifically, in the web server configuration, there's a directive, which indicates what filename to search for if only the directory name is specified. For example, in apache this can be

如果URL中只有路径,则无法从URL中获取文件名——不使用jQuery,不使用任何其他客户端方法。这是因为只有发送这个文件的服务器才知道这个文件是什么。具体地说,在web服务器配置中,有一个指令,指示如果只指定目录名,搜索什么文件名。例如,在apache中,这可以是

DirectoryIndex index.html index.php home.htm

This tells the server that for requests with only a directory name the server will attempt to serve file index.html from that directory; if it doesn't exist, then index.php; if that also doesn't exist then home.htm. If that one also doesn't exist, then the behaviour depends on other configuration options. Other web server software has similar configuration options.

这告诉服务器,对于只有目录名的请求,服务器将尝试为文件索引提供服务。从html目录;如果它不存在,那么index.php;如果那也不存在,那就回家。如果那个也不存在,那么行为取决于其他配置选项。其他web服务器软件也有类似的配置选项。

Hence, when you send a request like http://www.yoursite.com/folder/ to a server, only that server will know what file is actually used.

因此,当您向服务器发送类似http://www.yoursite.com/folder/的请求时,只有该服务器才知道实际使用的文件。