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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With