Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current file name from a url using javascript/jquery when only folder path is known

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

$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.

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

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

like image 976
lharby Avatar asked Jul 10 '13 16:07

lharby


1 Answers

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

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.

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.

like image 118
Aleks G Avatar answered Nov 02 '22 10:11

Aleks G