harrigaturu
harrigaturu

Reputation: 99

How can I find the current html filepage name using Javascript, on a LIVE-SERVER

I am going to deploy this page on an FTP And I need to find out how I can detect the html file currently being viewed using JavaScript.

If I open the html file, it works just fine with this:

var fileName = location.href.substring(location.href.lastIndexOf("/") +1);

But, if I open it via my localhost adress, it has a null value. So I'm guessing I have to use some other method to extract the current html file name. Or is there a better approach to this?

Note: I am not going to use JQuery or anything like that. EDIT: I can get the filename if it isn't my index file.. If it's the index file I get nothing using the above code. Most likely since all I have in my adress bar is the localhost adress of the live-server?

Upvotes: 0

Views: 138

Answers (1)

Quentin
Quentin

Reputation: 943510

The web deals in URLs, not file names.

Sometimes a URL will include something that looks like a file name, and sometimes that even maps on to a real file name on the server's hard disk.

When you type http://example.com/ then it might map that onto a file called index.html. Or maybe on to index.php. Or maybe it won't touch any file but will just use logic built into the web server application to determine what to respond with.

There's no way to know in the general case.

If your specific case, you know that the path / maps onto index.html, so you can write an explicit mapping in your JavaScript code.

Upvotes: 2

Related Questions